| 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>"Tools > Create Database Schema"</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 "Continue" - this will open a dialog with a SQL script and several generation options.</LI> |
| </UL> |
| |
| |
| <UL> |
| <LI>Leave the defaults on ("Create Tables", "Create FK Support", "Create Primary Key Support") and click "Generate" - 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>"Tools > Generate Classes"</TT> menu.</LI> |
| <LI>For "Type" select "Standard Persistent Objects" if it is not already selected.</LI> |
| <LI>For the output directory select "cayenne-tutorial/src" directory of your Eclipse project</LI> |
| <LI>Click "Generate"</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 "cayenne-tutorial" project and select "Refresh" - you should see a pair of classes generated for each mapped entity. Note that you should not modify the classes whose names start with "_" (underscore), as they will be replaced on subsequent generator runs. Instead all custom logic should be placed in their subclasses located in <TT>"cayenne.tutorial"</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> |