| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title xmlns:d="http://docbook.org/ns/docbook">Chapter 4. Creating Java Classes</title><link rel="stylesheet" type="text/css" href="css/cayenne-doc.css"><meta xmlns:d="http://docbook.org/ns/docbook" name="keywords" content="Cayenne 3.1 documentation"><meta xmlns:d="http://docbook.org/ns/docbook" name="description" content="User documentation for Apache Cayenne version 3.1"><link rel="home" href="index.html" title="Getting Started with Cayenne"><link rel="up" href="getting-started-part2.html" title="Part II. Learning mapping basics"><link rel="prev" href="ch03.html" title="Chapter 3. Getting started with Object Relational Mapping (ORM)"><link rel="next" href="getting-started-part3.html" title="Part III. Learning Cayenne API"><script xmlns:d="http://docbook.org/ns/docbook" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-7036673-1']); |
| _gaq.push(['_trackPageview']); |
| (function() { |
| var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| })(); |
| </script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div xmlns:d="http://docbook.org/ns/docbook" class="navheader"><table width="100%" summary="Navigation header"><tr><th class="versioninfo">v.3.1 (3.1)</th><th align="center">Chapter 4. Creating Java Classes</th><th></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><th width="60%" align="center"><a accesskey="u" href="getting-started-part2.html">Part II. Learning mapping basics</a></th><td width="20%" align="right"> <a accesskey="n" href="getting-started-part3.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 4. Creating Java Classes"><div class="titlepage"><div><div><h2 class="title"><a name="d0e264"></a>Chapter 4. Creating Java Classes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch04.html#creating-java-classes">Creating Java Classes</a></span></dt></dl></div><p>Here we'll generate the Java classes from the model that was created in the previous |
| section. CayenneModeler can be used to also generate the database schema, but since we |
| specified "CreateIfNoSchemaStrategy" earlier when we created a DataNode, we'll skip the |
| database schema step. Still be aware that you can do it if you need to via "Tools > |
| Create Database Schema". |
| </p><div class="section" title="Creating Java Classes"><div class="titlepage"><div><div><h2 class="title"><a name="creating-java-classes"></a>Creating Java Classes</h2></div></div></div><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>Select "Tools > Generate Classes" menu.</p></li><li class="listitem"><p>For "Type" select "Standard Persistent Objects", if it is not already |
| selected.</p></li><li class="listitem"><p>For the "Output Directory" select "src/main/java" folder under your Eclipse |
| project folder (this is a "peer" location to the cayenne-*.xml location we |
| selected before).</p></li><li class="listitem"><p>Click on "Entities" tab and check the "Check All Classes" checkbox (unless it |
| is already checked and reads "Uncheck all Classes").</p></li><li class="listitem"><p>Click "Generate"</p></li></ul></div><p>Now go back to Eclipse, right click on "tutorial" project and select "Refresh" - you |
| should see pairs of classes generated for each mapped entity. You probably also see that |
| there's a bunch of red squiggles next to the newly generated Java classes in Eclipse. |
| This is because our project does not include Cayenne as a Maven dependency yet. Let's |
| fix it now by adding "cayenne-server" artifact in the bottom of the pom.xml file. The |
| resulting POM should look like |
| this:</p><pre class="programlisting"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| <groupId>org.example.cayenne</groupId> |
| <artifactId>tutorial</artifactId> |
| <version>0.0.1-SNAPSHOT</version> |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.apache.cayenne</groupId> |
| <artifactId>cayenne-server</artifactId> |
| <!-- Here specify the version of Cayenne you are actually using --> |
| <version>3.1M3</version> |
| </dependency> |
| </dependencies> |
| </project></pre><p>Your computer must be connected to the internet. Once you save the pom.xml, Eclipse |
| will download the needed Cayenne jar file and add it to the project build path. As a |
| result, all the errors should disappear.</p><p><span class="inlinemediaobject"><img src="images/eclipse-generatedclasses.png"></span></p><p>Now let's check the entity class pairs. Each one is made of a superclass (e.g. Artist) |
| and a subclass (e.g. Artist). You <span class="bold"><strong>should not</strong></span> modify the |
| superclasses whose names start with "_" (underscore), as they will be replaced on |
| subsequent generator runs. Instead all custom logic should be placed in the subclasses |
| in "org.example.cayenne.persistent" package - those will never be overwritten by the |
| class generator.</p><p> |
| </p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p><span class="bold"><strong>Class Generation |
| Hint</strong></span> Often you'd start by generating classes from the |
| Modeler, but at the later stages of the project the generation is usually |
| automated either via Ant cgen task or Maven cgen mojo. All three methods are |
| interchangeable, however Ant and Maven methods would ensure that you never |
| forget to regenerate classes on mapping changes, as they are integrated into |
| the build cycle.</p></div><p> |
| </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="getting-started-part2.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="getting-started-part3.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 3. Getting started with Object Relational Mapping (ORM) </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Part III. Learning Cayenne API</td></tr></table></div></body></html> |