blob: 5c591e2c711b465e7be05bcc5fa3be4e9779ed11 [file] [log] [blame]
Title: Tutorial Object Relational Mapping
<P>The goal of this section is to learn how to create a simple Object-Relational model with CayenneModeler. We will create a complete ORM model for the following database 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>
<DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>Very often you'd have an existing database already, and it can be quickly imported in Cayenne via <TT>&quot;Tools &gt; Reengineer Database Schema&quot;</TT>. This will save you lots of time compared to manual mapping. However understanding how to create the mapping by hand is important, so we are showing the &quot;manual&quot; approach below.</TD></TR></TABLE></DIV>
<H3><A name="TutorialObjectRelationalMapping-MappingDatabaseTablesandColumns"></A>Mapping Database Tables and Columns</H3>
<P>Lets go back to CayenneModeler where we have the newly created project open and start by adding the ARTIST table. Database tables are called <B>&quot;DbEntities&quot;</B> in Cayenne mapping (those can be actual tables or database views). </P>
<P>Select &quot;UntitledDomainMap&quot; on the left-hand side project tree and click &quot;Create DbEntity&quot; button (or use <TT>&quot;Project &gt; Create DbEntity&quot;</TT> menu). A new DbEntity is created. In &quot;DbEntity Name&quot; field enter &quot;ARTIST&quot;. Then click on &quot;Create Attribute&quot; button on the entity toolbar (third button from the left). 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.png" style="border: 0px solid black"></SPAN></P>
<P>Similarly add NAME VARCHAR(200) and DATE_OF_BIRTH DATE attributes. After that repeat this procedure for PAINTING and GALLERY entities to match DB schema shown above.</P>
<DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>Don't forget to save your project periodically to avoid losing your work. You will also have to refresh the project in Eclipse after every CayenneModeler save, as Eclipse is by default unaware of any changes made in the Modeler.</TD></TR></TABLE></DIV>
<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 (second button from the left) - 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-dbrelationship.png" 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 back. Still you may have noticed one thing is missing - &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>Finally, do the same thing for &quot;PAINTING&quot;.</LI>
</UL>
<P>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. </P>
<UL>
<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-java-classes.html" title="Tutorial Java Classes">Tutorial Java Classes</A></B></P>
<HR>