blob: c381cfee49871828321103279de45c7574cd387c [file] [log] [blame]
Title: Tutorial Object Relational Mapping
<P>Lets go back to CayenneModeler where we have the newly created project open. Our goal is to map the following schema:</P>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-object-relational-mapping.data/database-schema.jpg" style="border: 0px solid black"></SPAN></P>
<H3><A name="TutorialObjectRelationalMapping-MappingDatabaseTables"></A>Mapping Database Tables</H3>
<P>Start by adding the ARTIST table. Mapped database tables are called &quot;DbEntities&quot; in Cayenne. Click on &quot;Create DbEntity&quot; button (or use <TT>&quot;Project &gt; Create DbEntity&quot;</TT> menu). In &quot;DbEntity Name&quot; field enter &quot;ARTIST&quot;. Then click on &quot;Create Attribute&quot; button on the entity toolbar (second button on the right). This action changes the view to the &quot;Attribute&quot; tab and adds a new attribute (attribute means a &quot;table column&quot; in this case) called &quot;untitledAttr&quot;. Let's rename it to ID, make it an INTEGER and make it a PK:</P>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-object-relational-mapping.data/modeler-artistid.jpg" style="border: 0px solid black"></SPAN></P>
<P>Similarly add NAME VARCHAR and DATE_OF_BIRTH DATE attributes. </P>
<P>Repeat this procedure for PAINTING and GALLERY entities.</P>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-object-relational-mapping.data/modeler-gallery.jpg" style="border: 0px solid black"></SPAN></P>
<H3><A name="TutorialObjectRelationalMapping-MappingDatabaseRelationships"></A>Mapping Database Relationships</H3>
<P>Now we need to specify relationships between ARTIST, PAINTING and GALLERY tables. Start by creating a one-to-many ARTIST/PAINTING relationship:</P>
<UL>
<LI>Select the ARTIST DbEntity on the left and click on the &quot;Relationships&quot; tab.</LI>
<LI>Click on &quot;Create Relationship&quot; button on the entity toolbar - a relationship called &quot;untitledRel&quot; is created.</LI>
<LI>Choose the &quot;Target&quot; to be &quot;Painting&quot;.</LI>
<LI>Click on the &quot;Database Mapping&quot; button (letter &quot;I&quot; in a circle) - relationship configuration dialog is presented. Here you can assign a name to the relationship and also its complimentary reverse relationship. This name can be anything (this is really a symbolic name of the database referential constraint), but it is recommended to use a valid Java identifier, as this will save some typing later. We'll call the relationship &quot;paintings&quot; and reverse relationship &quot;artist&quot;.</LI>
<LI>Click on &quot;Add&quot; button on the right to add a join</LI>
<LI>Select &quot;ID&quot; column for the &quot;Source&quot; and &quot;ARTIST_ID&quot; column for the target.</LI>
<LI>Relationship information should now look like this:</LI>
</UL>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-object-relational-mapping.data/modeler-dbrel-dialog.jpg" style="border: 0px solid black"></SPAN></P>
<UL>
<LI>Click &quot;Done&quot; to confirm the changes and close the dialog.</LI>
<LI>Two complimentary relationships have been created - from ARTIST to PAINTING and vice versa. Still you may have noticed one glitch - &quot;paintings&quot; relationship should be to-many, but &quot;To Many&quot; checkbox is not checked. Let's change that - check the checkbox for &quot;paintings&quot; relationship, and then click on PAINTING DbEntity, and uncheck &quot;artist&quot; relationship &quot;To Many&quot; to make the reverse relationship &quot;to-one&quot; as it should be.</LI>
<LI>Repeat the steps above to create a many-to-one relationship from PAINTING to GALLERY, calling the relationships pair &quot;gallery&quot; and &quot;paintings&quot;.</LI>
</UL>
<H3><A name="TutorialObjectRelationalMapping-MappingJavaClasses"></A>Mapping Java Classes</H3>
<P>Now that the database schema mapping is complete, CayenneModeler can create mappings of Java classes (aka &quot;ObjEntities&quot;) by deriving everything from DbEntities. At present there is no way to do it for the entire DataMap in one click, so we'll do it for each table individually.</P>
<UL>
<LI>Select &quot;ARTIST&quot; DbEntity and click on &quot;Create ObjEntity&quot; button (a green class icon) either on the entity toolbar or on the main toolbar. An ObjEntity called &quot;Artist&quot; is created with a Java class field set to &quot;cayenne.tutorial.Artist&quot;. The modeler transformed the database names to the Java-friendly names (e.g., if you click on the &quot;Attributes&quot; tab, you'll see that &quot;DATE_OF_BIRTH&quot; column was converted to &quot;dateOfBirth&quot; Java class attribute).</LI>
<LI>Select &quot;GALLERY&quot; DbEntity and click on &quot;Create ObjEntity&quot; button again - you'll see a &quot;Gallery&quot; ObjEntity created.</LI>
<LI>Do the same thing again for &quot;PAINTING&quot;.<BR>
Now you need to synchronize relationships. Artist and Gallery entities were created when there was no related &quot;Painting&quot; entity, so their relationships were not set. </LI>
<LI>Click on the &quot;Artist&quot; ObjEntity and (optinally) change to the &quot;Relationships&quot; tab. Now click on &quot;Sync ObjEntity with DbEntity&quot; button on the toolbar (two yellow arrows) - you will see the &quot;paintings&quot; relationship appear.</LI>
<LI>Do the same for the &quot;Gallery&quot; entity.</LI>
</UL>
<P>Unless you want to customize the Java class and property names (which you can do easily) the mapping is complete. </P>
<HR>
<P><B>Next Step: <A href="tutorial-generate-database-and-java-classes.html" title="Tutorial Generate Database and Java Classes">Tutorial Generate Database and Java Classes</A></B></P>
<HR>