blob: bcfd839c0fbb94fc17b9f0175e7ed56f8055f5b2 [file] [log] [blame]
Title: Tutorial Generate Database and Java Classes
<P>CayenneModeler can be used to generate both database schema and Java classes from the single XML model. Very often the database already exists and is maintained independently (e.g., by a DBA). In this case you won't have to go through the first part of this procedure.</P>
<H3><A name="TutorialGenerateDatabaseandJavaClasses-CreatingtheDatabase"></A>Creating the Database</H3>
<UL>
<LI>Click on the DataMap node on the left and then select <TT>&quot;Tools &gt; Create Database Schema&quot;</TT> from the menu. A connection dialog pops up with fields prefilled with the data you entered for the DataNode.</LI>
</UL>
<UL>
<LI>Leave it unchanged and click &quot;Continue&quot; - this will open a dialog with a SQL script and several generation options.</LI>
</UL>
<UL>
<LI>Leave the defaults on (&quot;Create Tables&quot;, &quot;Create FK Support&quot;, &quot;Create Primary Key Support&quot;) and click &quot;Generate&quot; - the database with the tables mapped during previous steps will be created in the specified location. Since Derby uses a filesystem directory for the database, you can actually navigate to it and see that the database is there.</LI>
</UL>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-generate-database-and-java-classes.data/db-generator.jpg" style="border: 0px solid black"></SPAN></P>
<H3><A name="TutorialGenerateDatabaseandJavaClasses-CreatingtheJavaClasses"></A>Creating the Java Classes</H3>
<UL>
<LI>Now select <TT>&quot;Tools &gt; Generate Classes&quot;</TT> menu.</LI>
<LI>For &quot;Type&quot; select &quot;Standard Persistent Objects&quot; if it is not already selected.</LI>
<LI>For the output directory select &quot;cayenne-tutorial/src&quot; directory of your Eclipse project</LI>
<LI>Click &quot;Generate&quot;</LI>
</UL>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-generate-database-and-java-classes.data/class-generator.jpg" style="border: 0px solid black"></SPAN></P>
<P>Note that often you start by generating classes from the Modeler, but at the later stages of the project the generation is usually automated via <A href="cgen.html" title="cgen">Ant cgen task</A>. Both methods are interchangeable.</P>
<P>Now go back to Eclipse, right click on &quot;cayenne-tutorial&quot; project and select &quot;Refresh&quot; - you should see a pair of classes generated for each mapped entity. Note that you should not modify the classes whose names start with &quot;_&quot; (underscore), as they will be replaced on subsequent generator runs. Instead all custom logic should be placed in their subclasses located in <TT>&quot;cayenne.tutorial&quot;</TT> package - those will never be overwritten by the class generator.</P>
<P><SPAN class="image-wrap" style=""><IMG src="tutorial-generate-database-and-java-classes.data/eclipse-classes.jpg" style="border: 0px solid black"></SPAN></P>
<HR>
<P><B>Next Step: <A href="tutorial-datacontext.html" title="Tutorial DataContext">Tutorial DataContext</A></B></P>
<HR>