blob: 2cee05f8f54c205be5d6682b4b137a556c6ff864 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title xmlns:d="http://docbook.org/ns/docbook">Chapter&nbsp;2.&nbsp;Starting a project</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&nbsp;II.&nbsp;Learning mapping basics"><link rel="prev" href="getting-started-part2.html" title="Part&nbsp;II.&nbsp;Learning mapping basics"><link rel="next" href="ch03.html" title="Chapter&nbsp;3.&nbsp;Getting started with Object Relational Mapping (ORM)"><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&nbsp;2.&nbsp;Starting a project</th><th></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="getting-started-part2.html">Prev</a>&nbsp;</td><th width="60%" align="center"><a accesskey="u" href="getting-started-part2.html">Part&nbsp;II.&nbsp;Learning mapping basics</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch03.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter&nbsp;2.&nbsp;Starting a project"><div class="titlepage"><div><div><h2 class="title"><a name="d0e58"></a>Chapter&nbsp;2.&nbsp;Starting a project</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch02.html#create-new-project">Create a new Project in Eclipse </a></span></dt><dt><span class="section"><a href="ch02.html#download-and-start-cayenne-modeler">Download and Start CayenneModeler</a></span></dt><dt><span class="section"><a href="ch02.html#create-new-mapping-project">Create a New Mapping Project in CayenneModeler</a></span></dt><dt><span class="section"><a href="ch02.html#create-datanode">Create a DataNode</a></span></dt><dt><span class="section"><a href="ch02.html#create-datamap">Create a DataMap</a></span></dt><dt><span class="section"><a href="ch02.html#save-project">Save the Project</a></span></dt></dl></div><p>
The goal of this chapter is to create a new Java project in Eclipse
containing a basic Cayenne mapping. It presents an introduction to
CayenneModeler GUI tool, showing how to create the initial mapping
objects: DataDomain, DataNode, DataMap.
</p><div class="section" title="Create a new Project in Eclipse"><div class="titlepage"><div><div><h2 class="title"><a name="create-new-project"></a>Create a new Project in Eclipse </h2></div></div></div><p>
In Eclipse select "File &gt; New &gt; Other..." and then "Maven &gt;
Maven Project". Click "Next". On the following screen check
"Create a simple project" checkbox and click "Next" again.
In the dialog shown on the screenshot below, fill the "Group Id"
and "Artifact Id" fields and click "Finish".
</p><p>
<span class="inlinemediaobject"><img src="images/tutorial-eclipse-project.png"></span>
</p><p>
Now you should have a new empty project in the Eclipse workspace.
Check that the project Java compiler settings are correct. Rightclick
on the "tutorial" project, select "Properties &gt; Java Compiler" and
ensure that "Compiler compliance level" is at least "1.5"
(some versions of Maven plugin seem to be setting it to 1.4 by default).
</p></div><div class="section" title="Download and Start CayenneModeler"><div class="titlepage"><div><div><h2 class="title"><a name="download-and-start-cayenne-modeler"></a>Download and Start CayenneModeler</h2></div></div></div><p>
Although later in this tutorial we'll be using Maven to include Cayenne
runtime jars in the project, you'll still need to download Cayenne to
get access to the CayenneModeler tool.
</p><p>
</p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>If you are really into Maven, you can start
CayenneModeler from Maven too. We'll do it in a more traditional way
here.</p></div><p>
</p><p> Download the latest release. Unpack the distribution somewhere in the file system and
start CayenneModeler, following platform-specific instructions. On most platforms it is
done simply by doubleclicking the Modeler icon. The welcome screen of the Modeler looks
like this: </p><p><span class="inlinemediaobject"><img src="images/modeler-started.png"></span></p></div><div class="section" title="Create a New Mapping Project in CayenneModeler"><div class="titlepage"><div><div><h2 class="title"><a name="create-new-mapping-project"></a>Create a New Mapping Project in CayenneModeler</h2></div></div></div><p>Click on the "New Project" button on Welcome screen. A new mapping project will appear
that contains a single <span class="bold"><strong>DataDomain</strong></span>. The meaning of a
DataDomain is explained elsewhere in the User Guide. For now it is sufficient to
understand that DataDomain is the root of your mapping project.</p></div><div class="section" title="Create a DataNode"><div class="titlepage"><div><div><h2 class="title"><a name="create-datanode"></a>Create a DataNode</h2></div></div></div><p>The next project object you will create is a <span class="bold"><strong>DataNode</strong></span>. DataNode is a descriptor of a single database your application
will connect to. Cayenne mapping project can use more than one database, but for now,
we'll only use one. With "project" selected on the left, click on "Create DataNode"
button on the toolbar (or select "Project &gt; Create DataNode" from the menu.</p><p>A new DataNode is displayed. Now you need to specify JDBC connection parameters. For
an in-memory Derby database you can enter the following settings: </p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>JDBC Driver: org.apache.derby.jdbc.EmbeddedDriver</p></li><li class="listitem"><p>DB URL: jdbc:derby:memory:testdb;create=true</p></li></ul></div><p>
</p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>We are creating an in-memory database here. So when
you stop your application, all the data will be lost. In most real-life
cases you'll be connecting to a database that actually persists its data on
disk, but an in-memory DB will do for the simple tutorial.</p></div><p>
</p><p>Also you will need to change "Schema Update Strategy". Select
"org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy" from the dropdown, so that
Cayenne creates a new schema on Derby based on the ORM mapping when the application
starts.</p><p><span class="inlinemediaobject"><img src="images/base-datanode.png"></span></p></div><div class="section" title="Create a DataMap"><div class="titlepage"><div><div><h2 class="title"><a name="create-datamap"></a>Create a DataMap</h2></div></div></div><p>Now you will create a <span class="bold"><strong>DataMap</strong></span>. DataMap is an object
that holds all the mapping information. To create it, click on "Create DataMap" button
(or select a corresponding menu item). Note that the newly created DataMap is
automatically linked to the DataNode that you created in the previous step. If there is
more than one DataNode, you may need to link a DataMap to the correct node manually. In
other words a DataMap within DataDomain must point to a database described by the
map.</p><p>You can leave all the DataMap defaults unchanged except for one - "Java Package".
Enter "org.example.cayenne.persistent". This name will later be used for all persistent
classes.</p><p><span class="inlinemediaobject"><img src="images/base-datamap.png"></span></p></div><div class="section" title="Save the Project"><div class="titlepage"><div><div><h2 class="title"><a name="save-project"></a>Save the Project</h2></div></div></div><p>Before you proceed with the actual mapping, let's save the project. Click on "Save"
button in the toolbar and navigate to the "tutorial" Eclipse project folder that was
created earlier in this section and its "src/main/resources" subfolder and save the
project there. Now go back to Eclipse, right click on "tutorial" project and select
"Refresh", you will see three Cayenne XML files.</p><p><span class="inlinemediaobject"><img src="images/eclipse-xmlfiles.png"></span></p><p>Note that the location of the XML files is not coincidental. Cayenne runtime looks for
"cayenne-*.xml" file in the application CLASSPATH and "src/main/resources" folder should
already be a "class folder" in Eclipse for our project (and is also a standard location
that Maven would copy to a jar file, if we were using Maven from command-line).</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="getting-started-part2.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="getting-started-part2.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Part&nbsp;II.&nbsp;Learning mapping basics&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;3.&nbsp;Getting started with Object Relational Mapping (ORM)</td></tr></table></div></body></html>