cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Tutorial Object Relational Mapping
Date Sun, 20 Dec 2009 17:49:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><s>Tutorial Object Relational Mapping</s></h2>
     <h4>Page <b>removed</b> by             <a href="http://cwiki.apache.org/confluence/display/~andrus">Andrus
Adamchik</a>
    </h4>
     <br/>
     <div class="notificationGreySide">
         
<p>Lets go back to CayenneModeler where we have the newly created project open. Our
goal is to map the following schema:</p>

<p><img src="/confluence/download/attachments/10547/database-schema.jpg" align="absmiddle"
border="0" /></p>

<h3><a name="TutorialObjectRelationalMapping-MappingDatabaseTables"></a>Mapping
Database Tables</h3>

<p>Start by adding the ARTIST table. Mapped database tables are called "DbEntities"
in Cayenne. Click on "Create DbEntity" button (or use <tt>"Project &gt; Create DbEntity"</tt>
menu). In "DbEntity Name" field enter "ARTIST". Then click on "Create Attribute" button on
the entity toolbar (second button on the right). This action changes the view to the "Attribute"
tab and adds a new attribute (attribute means a "table column" in this case) called "untitledAttr".
Let's rename it to ID, make it an INTEGER and make it a PK:</p>

<p><img src="/confluence/download/attachments/10547/modeler-artistid.jpg" align="absmiddle"
border="0" /></p>

<p>Similarly add NAME VARCHAR and DATE_OF_BIRTH DATE attributes. </p>

<p>Repeat this procedure for PAINTING and GALLERY entities.</p>

<p><img src="/confluence/download/attachments/10547/modeler-gallery.jpg" align="absmiddle"
border="0" /></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 "Relationships" tab.</li>
	<li>Click on "Create Relationship" button on the entity toolbar - a relationship called
"untitledRel" is created.</li>
	<li>Choose the "Target" to be "Painting".</li>
	<li>Click on the "Database Mapping" button (letter "I" 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 "paintings" and reverse relationship
"artist".</li>
	<li>Click on "Add" button on the right to add a join</li>
	<li>Select "ID" column for the "Source" and "ARTIST_ID" column for the target.</li>
	<li>Relationship information should now look like this:</li>
</ul>


<p><img src="/confluence/download/attachments/10547/modeler-dbrel-dialog.jpg" align="absmiddle"
border="0" /></p>

<ul>
	<li>Click "Done" 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 - "paintings" relationship should be to-many,
but "To Many" checkbox is not checked. Let's change that - check the checkbox for "paintings"
relationship, and then click on PAINTING DbEntity, and uncheck "artist" relationship "To Many"
to make the reverse relationship "to-one" 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 "gallery" and "paintings".</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 "ObjEntities") 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 "ARTIST" DbEntity and click on "Create ObjEntity" button (a green class
icon) either on the entity toolbar or on the main toolbar. An ObjEntity called "Artist" is
created with a Java class field set to "cayenne.tutorial.Artist". The modeler transformed
the database names to the Java-friendly names (e.g., if you click on the "Attributes" tab,
you'll see that "DATE_OF_BIRTH" column was converted to "dateOfBirth" Java class attribute).</li>
	<li>Select "GALLERY" DbEntity and click on "Create ObjEntity" button again - you'll
see a "Gallery" ObjEntity created.</li>
	<li>Do the same thing again for "PAINTING".<br/>
Now you need to synchronize relationships. Artist and Gallery entities were created when there
was no related "Painting" entity, so their relationships were not set. </li>
	<li>Click on the "Artist" ObjEntity and (optinally) change to the "Relationships" tab.
Now click on "Sync ObjEntity with DbEntity" button on the toolbar (two yellow arrows) - you
will see the "paintings" relationship appear.</li>
	<li>Do the same for the "Gallery" 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="/confluence/display/CAYDOC/Tutorial+Generate+Database+and+Java+Classes"
title="Tutorial Generate Database and Java Classes">Tutorial Generate Database and Java
Classes</a></b></p>
<hr />
     </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message