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 th...@apache.org on 2003/03/05 22:22:08 UTC
cvs commit: db-ojb/xdocs faq.xml
thma 2003/03/05 13:22:08
Modified: xdocs faq.xml
Log:
add Charles Anthonies example for ternary relationships
Revision Changes Path
1.15 +178 -0 db-ojb/xdocs/faq.xml
Index: faq.xml
===================================================================
RCS file: /home/cvs/db-ojb/xdocs/faq.xml,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- faq.xml 6 Feb 2003 01:25:18 -0000 1.14
+++ faq.xml 5 Mar 2003 21:22:08 -0000 1.15
@@ -63,6 +63,7 @@
<li><a href="#28">Global metadata changes at runtime?</a></li>
<li><a href="#29">Per thread metadata changes at runtime?</a></li>
<li><a href="#30">Is it possible to use OJB within EJB's?</a></li>
+ <li><a href="#36">Can OJB handle ternary (or higher) associations?</a></li>
</ul>
@@ -921,6 +922,183 @@
Additional you can find some EJB example beans in package <code>org.apache.ojb.ejb</code>
under <code>[jakarta-ojb]/src/ejb</code>.
</p>
+ </subsection>
+
+ <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.
+ 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
+ by an intermediary table with 2 columns).
+ </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>
+ <source><![CDATA[
+public class ProjectRelationship {
+ Integer developerId;
+ Integer languageId;
+ Integer projectId;
+
+ Developer developer;
+ Language lanuage;
+ Project project;
+
+ /** setters and getters not shown for brevity**/
+}
+ ]]></source>
+ <p>
+ Here is the respective extract from the repository :
+ </p>
+ <source><![CDATA[
+<class-descriptor
+ class="ProjectRelationship"
+ table="PROJECTRELATIONSHIP"
+>
+ <field-descriptor
+ name="developerId"
+ column="DEVELOPER_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ />
+ <field-descriptor
+ name="languageId"
+ column="LANGUAGE_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ />
+ <field-descriptor
+ name="projectId"
+ column="PROJECT_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ />
+ <reference-descriptor
+ name="developer"
+ class-ref="Developer"
+ >
+ <foreignkey field-id-ref="developerId" />
+ </reference-descriptor>
+ <reference-descriptor
+ name="language"
+ class-ref="Language"
+ >
+ <foreignkey field-id-ref="languageId" />
+ </reference-descriptor>
+ <reference-descriptor
+ name="project"
+ class-ref="Project"
+ >
+ <foreignkey field-ref="projectId" />
+ </reference-descriptor>
+</class-descriptor>
+ ]]></source>
+
+<p>
+Here is some sample code for storing a relationship :
+</p>
+<source><![CDATA[
+Developer dev = .... ; // create or retrieve
+Project proj = .... ; // create or retrieve
+Language lang = .... ; // create or retrieve
+
+ProjectRelationship rel = new ProjectRelationship();
+rel.setDeveloper(dev);
+rel.setLanguage(lang);
+rel.setProject(proj);
+
+broker.store(r);
+]]></source>
+
+<p>
+In the next code sample we are looking up all
+Projects that Developer "Bob" has done in "Java".
+</p>
+<source><![CDATA[
+Criteria criteria = new Criteria();
+criteria.addEqualTo("developer.name","Bob");
+cirteria.addEquatTo("language.name","Java");
+
+Query q = new QueryByCriteria(ProjectRelationship.class, criteria, true);
+Iterator iter = Broker.getIteratorByQuery(q);
+
+// now iterate over the collection and retrieve all projects:
+while (iter.hasNext())
+{
+ ProjectRelationship rel = (ProjectRelationship) iter.next();
+ System.out.println(rel.getProject().toString());
+}
+]]></source>
+
+<p>
+You could also have on the Project class-descriptor a
+<code>collection-descriptor</code> that returns all relationships associated with the
+Project. If it was call "projectRelationships" the following would give you
+all projects that have a relationship with "bob" and the language "java".
+</p>
+
+<source><![CDATA[
+Criteria criteria = new Criteria();
+criteria.addEqualTo("projectRelationships.developer.name","bob");
+cirteria.addEquatTo("projectRelationships.language.name","java");
+
+Query q = new QueryByCriteria(Project.class, criteria, true);
+Collection projects = Broker.getCollectionByQuery(q);
+]]></source>
+
+<p>
+This is the layout of the Project class:
+</p>
+<source><![CDATA[
+public class Project {
+ Integer id;
+ String name;
+ Collection projectRelationships;
+
+ /** setters and getters not shown for brevity**/
+}
+]]></source>
+
+<p>
+This is the class-descriptor of the Project class:
+</p>
+<source><![CDATA[
+<class-descriptor
+ class="Project"
+ table="PROJECT"
+>
+ <field-descriptor
+ name="id"
+ column="ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ />
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+ <collection-descriptor
+ name="projectRelationships"
+ element-class-ref="ProjectRelationship"
+ >
+ <inverse-foreignkey field-ref="projectId" />
+ </collection-descriptor>
+</class-descriptor>
+]]></source>
+
</subsection>