You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/05/15 10:01:38 UTC
cvs commit: db-ojb/xdocs faq.xml
arminw 2003/05/15 01:01:38
Modified: xdocs faq.xml
Log:
update outdated faq
'I don't like OQL, can I use the PersistenceBroker Queries...'
Revision Changes Path
1.17 +74 -94 db-ojb/xdocs/faq.xml
Index: faq.xml
===================================================================
RCS file: /home/cvs/db-ojb/xdocs/faq.xml,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- faq.xml 18 Apr 2003 11:20:38 -0000 1.16
+++ faq.xml 15 May 2003 08:01:38 -0000 1.17
@@ -616,53 +616,32 @@
Several users (including myself) are doing this.
</p>
<p>
- If you have a look at the <code>execute()</code> method in <code>org.apache.ojb.odmg.oql.OqlQueryImpl</code>
+ If you have a look at the simple example below
you will see how OJB Query objects can be used withing ODMG transactions.
- The most important thing is to lock all objects returned by a query to the current transaction.
+ <br/>
+ The most important thing is to lock all objects returned by a
+ query to the current transaction before starting manipulating
+ these objects.
+ <br/>
+ Further on do not commit or close the obtained PB-instance, this will be done
+ by the ODMG transaction on <code>tx.commit() / tx.rollback()</code>.
</p>
<source>
-public Object execute() throws org.odmg.QueryException
-{
- try
- {
- //obtain current ODMG transaction
- Transaction tx =OJBFactory.getInstance().currentTransaction();
-
- ...
-
- // obtain a broker instance from the current transaction
- PersistenceBroker broker = ((HasBroker) tx).getBroker();
-
- broker.beginTransaction();
- // ask the broker to perfom the query.
- // the concrete result type is configurable
- ManageableCollection result =
- (ManageableCollection) broker.getCollectionByQuery(
- this.getCollectionClass(),
- query);
- broker.commitTransaction();
-
- // read-lock all resulting objects to the current transaction
- Iterator iter = result.ojbIterator();
- Object toBeLocked = null;
- while (iter.hasNext())
- {
- toBeLocked = iter.next();
- //we can only lock objects, not attributes
- if (broker.hasClassDescriptor(toBeLocked.getClass()))
- tx.lock(toBeLocked, Transaction.READ);
- }
-
- ...
-
- return result;
- }
- catch (Throwable t)
- {
- OJB.getLogger().error(t);
- throw new org.odmg.QueryException(t.getMessage());
- }
-}
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
+ ....
+ // cast to get intern used PB instance
+ PersistenceBroker broker = ((HasBroker) tx).getBroker();
+ ...
+ // build query
+ QueryByCriteria query = ...
+ // perform PB-query
+ Collection result = broker.getCollectionByQuery(query);
+ // use result
+ ...
+
+ tx.commit();
+ ...
</source>
</subsection>
@@ -928,78 +907,78 @@
<subsection name="Can OJB handle ternary (or higher) associations?" anchor="36">
<p>
Yes, that's possible. Here is an example.
- With a ternary relationship there are three (or more) entities
- 'related' to each other.
+ With a ternary relationship there are three (or more) entities
+ 'related' to each other.
An example would be <code>Developer</code>, <code>Language</code> and <code>Project</code>.
</p>
- <p>
- Each entity is mapped to one table (<code>DEVELOPER</code>, <code>LANGUAGE</code> and
- <code>PROJECT</code>). To represent the combinations
- of these entities we need an additional bridge table
- (<code>PROJECTRELATIONSHIP</code>)with three
- columns holding the foreign keys to
- the other three tables (just like an m:n association is represented
+ <p>
+ Each entity is mapped to one table (<code>DEVELOPER</code>, <code>LANGUAGE</code> and
+ <code>PROJECT</code>). To represent the combinations
+ of these entities we need an additional bridge table
+ (<code>PROJECTRELATIONSHIP</code>)with three
+ columns holding the foreign keys to
+ the other three tables (just like an m:n association is represented
by an intermediary table with 2 columns).
</p>
- <p>
+ <p>
To handle this table with OJB we have to define a class that is mapped on it.
This Relationship class can then be used to perform
queries/updates as with any other persistent class. Here is the layout
- of this class:
- </p>
+ of this class:
+ </p>
<source><![CDATA[
public class ProjectRelationship {
Integer developerId;
Integer languageId;
Integer projectId;
-
- Developer developer;
+
+ Developer developer;
Language lanuage;
Project project;
-
+
/** setters and getters not shown for brevity**/
}
]]></source>
- <p>
+ <p>
Here is the respective extract from the repository :
</p>
<source><![CDATA[
-<class-descriptor
- class="ProjectRelationship"
+<class-descriptor
+ class="ProjectRelationship"
table="PROJECTRELATIONSHIP"
>
- <field-descriptor
- name="developerId"
+ <field-descriptor
+ name="developerId"
column="DEVELOPER_ID"
jdbc-type="INTEGER"
primarykey="true"
/>
- <field-descriptor
- name="languageId"
+ <field-descriptor
+ name="languageId"
column="LANGUAGE_ID"
- jdbc-type="INTEGER"
+ jdbc-type="INTEGER"
primarykey="true"
/>
- <field-descriptor
- name="projectId"
+ <field-descriptor
+ name="projectId"
column="PROJECT_ID"
- jdbc-type="INTEGER"
+ jdbc-type="INTEGER"
primarykey="true"
/>
- <reference-descriptor
- name="developer"
+ <reference-descriptor
+ name="developer"
class-ref="Developer"
>
<foreignkey field-id-ref="developerId" />
</reference-descriptor>
- <reference-descriptor
- name="language"
+ <reference-descriptor
+ name="language"
class-ref="Language"
>
<foreignkey field-id-ref="languageId" />
</reference-descriptor>
- <reference-descriptor
- name="project"
+ <reference-descriptor
+ name="project"
class-ref="Project"
>
<foreignkey field-ref="projectId" />
@@ -1012,7 +991,7 @@
</p>
<source><![CDATA[
Developer dev = .... ; // create or retrieve
-Project proj = .... ; // create or retrieve
+Project proj = .... ; // create or retrieve
Language lang = .... ; // create or retrieve
ProjectRelationship rel = new ProjectRelationship();
@@ -1067,7 +1046,7 @@
Integer id;
String name;
Collection projectRelationships;
-
+
/** setters and getters not shown for brevity**/
}
]]></source>
@@ -1076,27 +1055,27 @@
This is the class-descriptor of the Project class:
</p>
<source><![CDATA[
-<class-descriptor
- class="Project"
+<class-descriptor
+ class="Project"
table="PROJECT"
>
- <field-descriptor
- name="id"
+ <field-descriptor
+ name="id"
column="ID"
jdbc-type="INTEGER"
primarykey="true"
/>
- <field-descriptor
- name="name"
+ <field-descriptor
+ name="name"
column="NAME"
- jdbc-type="VARCHAR"
+ jdbc-type="VARCHAR"
/>
- <collection-descriptor
- name="projectRelationships"
+ <collection-descriptor
+ name="projectRelationships"
element-class-ref="ProjectRelationship"
>
<inverse-foreignkey field-ref="projectId" />
- </collection-descriptor>
+ </collection-descriptor>
</class-descriptor>
]]></source>
@@ -1109,21 +1088,22 @@
persistent entity class!
<br/>
Follow these steps to provide a mapping for an attribute holding alist of Strings.
- Let's assume your persistent class has an attribute <code>listOfStrings</code>
+ Let's assume your persistent class has an attribute <code>listOfStrings</code>
holding a list of Strings:
-<source><![CDATA[
+<source><![CDATA[
protected Collection listOfStrings;
]]></source>
-
- The database table mapped to the persistent class has a colum <code>LIST_OF_STRINGS</code> of type
+
+ The database table mapped to the persistent class has a colum <code>LIST_OF_STRINGS</code> of type
<code>VARCHAR</code> that is used to hold all strings.
-<source><![CDATA[
+<source><![CDATA[
<field-descriptor
name="listOfStrings"
column="LIST_OF_STRINGS"
jdbc-type="VARCHAR"
- conversion="o.a.ojb.broker.accesslayer.conversions.StringVector2VarcharFieldConversion"
+ conversion=
+"o.a.ojb.broker.accesslayer.conversions.StringVector2VarcharFieldConversion"
/>
]]></source>