cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [3/4] cayenne-website git commit: Add 3.1 docs
Date Fri, 05 Jan 2018 14:33:26 GMT
http://git-wip-us.apache.org/repos/asf/cayenne-website/blob/b81e46fb/src/main/site/content/docs/3.1/cayenne-guide.html
----------------------------------------------------------------------
diff --git a/src/main/site/content/docs/3.1/cayenne-guide.html b/src/main/site/content/docs/3.1/cayenne-guide.html
new file mode 100644
index 0000000..87006a4
--- /dev/null
+++ b/src/main/site/content/docs/3.1/cayenne-guide.html
@@ -0,0 +1,3600 @@
+---
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+title: "Cayenne Guide"
+description: "Cayenne Guide"
+cayenneVersion: "3.1"
+weight: 20
+---
+<div class="sect1"> 
+ <h2 id="object-relational-mapping-with-cayenne"><a class="anchor" href="#object-relational-mapping-with-cayenne"></a>1. Object Relational Mapping with Cayenne</h2> 
+ <div class="sectionbody"> 
+  <div class="sect2"> 
+   <h3 id="setup"><a class="anchor" href="#setup"></a>1.1. Setup</h3> 
+   <div class="sect3"> 
+    <h4 id="system-requirements"><a class="anchor" href="#system-requirements"></a>System Requirements</h4> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p>Java: Cayenne runtime framework and CayenneModeler GUI tool are written in 100% Java, and run on any Java-compatible platform. Required JDK version is 1.5 or higher. The last version of Cayenne compatible with JDK 1.4 is 1.2.x/2.0.x and JDK 1.3 is 1.1.x</p> </li> 
+      <li> <p>JDBC Driver: An appropriate DB-specific JDBC driver is needed to access the database. It can be included in the application or used in web container DataSource configuration.</p> </li> 
+      <li> <p>Third-party Libraries: Cayenne runtime framework has a minimal set of required and a few more optional dependencies on third-party open source packages. See "Including Cayenne in a Project" chapter for details.</p> </li> 
+     </ul> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="runModeler"><a class="anchor" href="#runModeler"></a>Running CayenneModeler</h4> 
+    <div class="paragraph"> 
+     <p>CayenneModeler GUI tool is intended to work with object relational mapping projects. While you can edit your XML by hand, it is rarely needed, as the Modeler is a pretty advanced tool included in Cayenne distribution. To obtain CayenneModeler, download Cayenne distribution archive from <a href="http://cayenne.apache.org/download.html" class="bare">http://cayenne.apache.org/download.html</a> matching the OS you are using. Of course Java needs to be installed on the machine where you are going to run the Modeler.</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p>OS X distribution contains CayenneModeler.app at the root of the distribution disk image.</p> </li> 
+      <li> <p>Windows distribution contains CayenneModeler.exe file in the bin directory.</p> </li> 
+      <li> <p>Cross-platform distribution (targeting Linux, but as the name implies, compatible with any OS) contains a runnable CayenneModeler.jar in the bin directory. It can be executed either by double-clicking, or if the environment is not configured to execute jars, by running from command-line:</p> </li> 
+     </ul> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre>$ java -jar CayenneModeler.jar</pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>The Modeler can also be started from Maven. While it may look like an exotic way to start a GUI application, it has its benefits - no need to download Cayenne distribution, the version of the Modeler always matches the version of the framework, the plugin can find mapping files in the project automatically. So it is an attractive option to some developers. Maven option requires a declaration in the POM:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-xml xml" data-lang="xml">&lt;build&gt;
+    &lt;plugins&gt;
+        &lt;plugin&gt;
+            &lt;groupId&gt;org.apache.cayenne.plugins&lt;/groupId&gt;
+            &lt;artifactId&gt;maven-cayenne-modeler-plugin&lt;/artifactId&gt;
+            &lt;version&gt;3.1.3-SNAPSHOT&lt;/version&gt;
+        &lt;/plugin&gt;
+    &lt;/plugins&gt;
+&lt;/build&gt;</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>And then can be run as</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre>$ mvn cayenne-modeler:run</pre> 
+     </div> 
+    </div> 
+   </div> 
+  </div> 
+  <div class="sect2"> 
+   <h3 id="cayenne-mapping-structure"><a class="anchor" href="#cayenne-mapping-structure"></a>1.2. Cayenne Mapping Structure</h3> 
+   <div class="sect3"> 
+    <h4 id="cayenne-project"><a class="anchor" href="#cayenne-project"></a>Cayenne Project</h4> 
+    <div class="paragraph"> 
+     <p>A Cayenne project is an XML representation of a model connecting database schema with Java classes. A project is normally created and manipulated via CayenneModeler GUI and then used to initialize Cayenne runtime. A project is made of one or more files. There’s always a root project descriptor file in any valid project. It is normally called <code>cayenne-xyz.xml</code>, where "xyz" is the name of the project.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Project descriptor can reference DataMap files, one per DataMap. DataMap files are normally called <code>xyz.map.xml</code>, where "xyz" is the name of the DataMap. For legacy reasons this naming convention is different from the convention for the root project descriptor above, and we may align it in the future versions. Here is how a typical project might look on the file system:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre>~: ls -l
+total 24
+-rw-r--r--  1 cayenne  staff  491 Jan 28 18:25 cayenne-project.xml
+-rw-r--r--  1 cayenne  staff  313 Jan 28 18:25 datamap.map.xml</pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>DataMap are referenced by name in the root descriptor:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-xml xml" data-lang="xml">&lt;map name="datamap"/&gt;</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Map files are resolved by Cayenne by appending ".map.xml" extension to the map name, and resolving the resulting string relative to the root descriptor URI. The following sections discuss varios ORM model objects, without regards to their XML representation. XML format details are really unimportant to the Cayenne users.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="datamap"><a class="anchor" href="#datamap"></a>DataMap</h4> 
+    <div class="paragraph"> 
+     <p>DataMap is a container of persistent entities and other object-relational metadata. DataMap provides developers with a scope to organize their entities, but it does not provide a namespace for entities. In fact all DataMaps present in runtime are combined in a single namespace. Each DataMap must be associated with a DataNode. This is how Cayenne knows which database to use when running a query.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="datanode"><a class="anchor" href="#datanode"></a>DataNode</h4> 
+    <div class="paragraph"> 
+     <p>DataNode is model of a database. It is actually pretty simple. It has an arbitrary user-provided name and information needed to create or locate a JDBC DataSource. Most projects only have one DataNode, though there may be any number of nodes if needed.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="dbentity"><a class="anchor" href="#dbentity"></a>DbEntity</h4> 
+    <div class="paragraph"> 
+     <p>DbEntity is a model of a single DB table or view. DbEntity is made of DbAttributes that correspond to columns, and DbRelationships that map PK/FK pairs. DbRelationships are not strictly tied to FK constraints in DB, and should be mapped for all logical "relationships" between the tables.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="objentity"><a class="anchor" href="#objentity"></a>ObjEntity</h4> 
+    <div class="paragraph"> 
+     <p>ObjEntity is a model of a single persistent Java class. ObjEntity is made of ObjAttributes and ObjRelationships. Both correspond to entity class properties. However ObjAttributes represent "simple" properties (normally things like String, numbers, dates, etc.), while ObjRelationships correspond to properties that have a type of another entity.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>ObjEntity maps to one or more DbEntities. There’s always one "root" DbEntity for each ObjEntity. ObjAttribiute maps to a DbAttribute or an Embeddable. Most often mapped DbAttribute is from the root DbEntity. Sometimes mapping is done to a DbAttribute from another DbEntity somehow related to the root DbEntity. Such ObjAttribute is called "flattened". Similarly ObjRelationship maps either to a single DbRelationship, or to a chain of DbRelationships ("flattened" ObjRelationship).</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>ObjEntities may also contain mapping of their lifecycle callback methods.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="embeddable"><a class="anchor" href="#embeddable"></a>Embeddable</h4> 
+    <div class="paragraph"> 
+     <p>Embeddable is a model of a Java class that acts as a single attribute of an ObjEntity, but maps to multiple columns in the database.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="procedure"><a class="anchor" href="#procedure"></a>Procedure</h4> 
+    <div class="paragraph"> 
+     <p>A model of a stored procedure in the database.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="query"><a class="anchor" href="#query"></a>Query</h4> 
+    <div class="paragraph"> 
+     <p>A model of a query. Cayenne allows queries to be mapped in Cayenne project, or created in the code. Depending on the circumstances the users may take one or the other approach.</p> 
+    </div> 
+   </div> 
+  </div> 
+  <div class="sect2"> 
+   <h3 id="cayennemodeler-application"><a class="anchor" href="#cayennemodeler-application"></a>1.3. CayenneModeler Application</h3> 
+   <div class="sect3"> 
+    <h4 id="working-with-mapping-projects"><a class="anchor" href="#working-with-mapping-projects"></a>Working with Mapping Projects</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="reverse-engineering-database"><a class="anchor" href="#reverse-engineering-database"></a>Reverse Engineering Database</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="generating-database-schema"><a class="anchor" href="#generating-database-schema"></a>Generating Database Schema</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="migrations"><a class="anchor" href="#migrations"></a>Migrations</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="generating-java-classes"><a class="anchor" href="#generating-java-classes"></a>Generating Java Classes</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="modeling-inheritance"><a class="anchor" href="#modeling-inheritance"></a>Modeling Inheritance</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="modeling-generic-persistent-classes"><a class="anchor" href="#modeling-generic-persistent-classes"></a>Modeling Generic Persistent Classes</h4> 
+    <div class="paragraph"> 
+     <p>Normally each ObjEntity is mapped to a specific Java class (such as Artist or Painting) that explicitly declare all entity properties as pairs of getters and setters. However Cayenne allows to map a completly generic class to any number of entities. The only expectation is that a generic class implements org.apache.cayenne.DataObject. So an ideal candidate for a generic class is CayenneDataObject, or some custom subclass of CayenneDataObject.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>If you don’t enter anything for Java Class of an ObjEntity, Cayenne assumes generic mapping and uses the following implicit rules to determine a class of a generic object. If DataMap "Custom Superclass" is set, runtime uses this class to instantiate new objects. If not, <code>org.apache.cayenne.CayenneDataObject</code> is used.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Class generation procedures (either done in the Modeler or with Ant or Maven) would skip entities that are mapped to CayenneDataObject explicitly or have no class mapping.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="mapping-objattributes-to-custom-classes"><a class="anchor" href="#mapping-objattributes-to-custom-classes"></a>Mapping ObjAttributes to Custom Classes</h4> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="modeling-primary-key-generation-strategy"><a class="anchor" href="#modeling-primary-key-generation-strategy"></a>Modeling Primary Key Generation Strategy</h4> 
+   </div> 
+  </div> 
+ </div> 
+</div> 
+<div class="sect1"> 
+ <h2 id="cayenne-framework"><a class="anchor" href="#cayenne-framework"></a>2. Cayenne Framework</h2> 
+ <div class="sectionbody"> 
+  <div class="sect2"> 
+   <h3 id="include"><a class="anchor" href="#include"></a>2.1. Including Cayenne in a Project</h3> 
+   <div class="sect3"> 
+    <h4 id="jar-files-and-dependencies"><a class="anchor" href="#jar-files-and-dependencies"></a>Jar Files and Dependencies</h4> 
+    <div class="paragraph"> 
+     <p>Cayenne distribution contains the following core runtime jars in the distribution lib directory:</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><code>cayenne-server-x.x.jar</code> - contains full Cayenne runtime (DI, adapters, DB access classes, etc.). Most applications will use only this file.</p> </li> 
+      <li> <p><code>cayenne-client-x.x.jar</code> - a subset of cayenne-server.jar trimmed for use on the client in an ROP application.</p> </li> 
+      <li> <p>Other <code>cayenne-* jars</code> - various Cayenne extensions.</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When using <code>cayenne-server-x.x.jar</code> you’ll need a few third party jars (all included in <code>lib/third-party</code> directory of the distribution):</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><a href="http://velocity.apache.org">Apache Velocity Template Engine</a>, version 1.6.x (and all its dependencies bundled with velocity-dep)</p> </li> 
+      <li> <p><a href="http://commons.apache.org/proper/commons-collections/">Apache Commons Collections</a>, version 3.2.1</p> </li> 
+      <li> <p><a href="http://commons.apache.org/proper/commons-logging/">Apache Commons Logging</a>, version 1.1</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Cayenne integrates with various caching, clustering and other frameworks. These optional integrations will require other third-party jars that the users will need to obtain on their own.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="maven-projects"><a class="anchor" href="#maven-projects"></a>Maven Projects</h4> 
+    <div class="paragraph"> 
+     <p>If you are using Maven, you won’t have to deal with figuring out the dependencies. You can simply include cayenne-server artifact in your POM:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-xml xml" data-lang="xml">&lt;dependency&gt;
+   &lt;groupId&gt;org.apache.cayenne&lt;/groupId&gt;
+   &lt;artifactId&gt;cayenne-server&lt;/artifactId&gt;
+   &lt;version&gt;3.1.3-SNAPSHOT&lt;/version&gt;
+&lt;/dependency&gt;</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Additionally Cayenne provides a Maven plugin with a set of goals to perform various project tasks, such as synching generated Java classes with the mapping, described in the following subsection. The full plugin name is <code>org.apache.cayenne.plugins:cayenne-maven-plugin</code>.</p> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="cgen"><a class="anchor" href="#cgen"></a>cgen</h5> 
+     <div class="paragraph"> 
+      <p><code>cgen</code> is a <code>cayenne-maven-plugin</code> goal that generates and maintains source (.java) files of persistent objects based on a DataMap. By default, it is bound to the generate-sources phase. If "makePairs" is set to "true" (which is the recommended default), this task will generate a pair of classes (superclass/subclass) for each ObjEntity in the DataMap. Superclasses should not be changed manually, since they are always overwritten. Subclasses are never overwritten and may be later customized by the user. If "makePairs" is set to "false", a single class will be generated for each ObjEntity.</p> 
+     </div> 
+     <div class="paragraph"> 
+      <p>By creating custom templates, you can use cgen to generate other output (such as web pages, reports, specialized code templates) based on DataMap information.</p> 
+     </div> 
+     <table id="tablecgen" class="tableblock frame-all grid-all spread table table-bordered"> 
+      <caption class="title">
+       Table 1. cgen required parameters
+      </caption> 
+      <colgroup> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 66.6668%;"> 
+      </colgroup> 
+      <thead> 
+       <tr> 
+        <th class="tableblock halign-left valign-top">Name</th> 
+        <th class="tableblock halign-left valign-top">Type</th> 
+        <th class="tableblock halign-left valign-top">Description</th> 
+       </tr> 
+      </thead> 
+      <tbody> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">map</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">File</p></td> 
+        <td class="tableblock halign-left valign-top">
+         <div>
+          <div class="paragraph"> 
+           <p>DataMap XML file which serves as a source of metadata for class generation. E.g.</p> 
+          </div> 
+          <div class="listingblock"> 
+           <div class="content"> 
+            <pre class="highlight"><code>${project.basedir}/src/main/resources/my.map.xml</code></pre> 
+           </div> 
+          </div>
+         </div></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">destDir</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">File</p></td> 
+        <td class="tableblock halign-left valign-top">
+         <div>
+          <div class="paragraph"> 
+           <p>Root destination directory for Java classes (ignoring their package names).</p> 
+          </div>
+         </div></td> 
+       </tr> 
+      </tbody> 
+     </table> 
+     <table id="cgenOptional" class="tableblock frame-all grid-all spread table table-bordered"> 
+      <caption class="title">
+       Table 2. cgen optional parameters
+      </caption> 
+      <colgroup> 
+       <col style="width: 22.2222%;"> 
+       <col style="width: 11.1111%;"> 
+       <col style="width: 66.6667%;"> 
+      </colgroup> 
+      <thead> 
+       <tr> 
+        <th class="tableblock halign-left valign-top">Name</th> 
+        <th class="tableblock halign-left valign-top">Type</th> 
+        <th class="tableblock halign-left valign-top">Description</th> 
+       </tr> 
+      </thead> 
+      <tbody> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">additionalMaps</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">File</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A directory that contains additional DataMap XML files that may be needed to resolve cross-DataMap relationships for the the main DataMap, for which class generation occurs.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">client</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Whether we are generating classes for the client tier in a Remote Object Persistence application. "False" by default.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">embeddableTemplate</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Location of a custom Velocity template file for Embeddable class generation. If omitted, default template is used.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">embeddableSuperTemplate</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Location of a custom Velocity template file for Embeddable superclass generation. Ignored unless "makepairs" set to "true". If omitted, default template is used.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">encoding</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Generated files encoding if different from the default on current platform. Target encoding must be supported by the JVM running the build. Standard encodings supported by Java on all platforms are US-ASCII, ISO-8859-1, UTF-8, UTF-16BE, UTF-16LE, UTF-16. See javadocs for java.nio.charset.Charset for more information.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">excludeEntities</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A comma-separated list of ObjEntity patterns (expressed as a perl5 regex) to exclude from template generation. By default none of the DataMap entities are excluded.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">includeEntities</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A comma-separated list of ObjEntity patterns (expressed as a perl5 regex) to include from template generation. By default all DataMap entities are included.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">makePairs</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">If "true" (a recommended default), will generate subclass/superclass pairs, with all generated code placed in superclass.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">mode</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Specifies class generator iteration target. There are three possible values: "entity" (default), "datamap", "all". "entity" performs one generator iteration for each included ObjEntity, applying either standard to custom entity templates. "datamap" performs a single iteration, applying DataMap templates. "All" is a combination of entity and datamap.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">overwrite</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Only has effect when "makePairs" is set to "false". If "overwrite" is "true", will overwrite older versions of generated classes.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">superPkg</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Java package name of all generated superclasses. If omitted, each superclass will be placed in the subpackage of its subclass called "auto". Doesn’t have any effect if either "makepairs" or "usePkgPath" are false (both are true by default).</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">superTemplate</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Location of a custom Velocity template file for ObjEntity superclass generation. Only has effect if "makepairs" set to "true". If omitted, default template is used.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">template</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Location of a custom Velocity template file for ObjEntity class generation. If omitted, default template is used.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">usePkgPath</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">If set to "true" (default), a directory tree will be generated in "destDir" corresponding to the class package structure, if set to "false", classes will be generated in "destDir" ignoring their package.</p></td> 
+       </tr> 
+      </tbody> 
+     </table> 
+     <div class="paragraph"> 
+      <p>Example - a typical class generation scenario, where pairs of classes are generated with default Maven source destination and superclass package:</p> 
+     </div> 
+     <div class="listingblock"> 
+      <div class="content"> 
+       <pre class="highlight"><code class="language-xml xml" data-lang="xml">&lt;plugin&gt;
+    &lt;groupId&gt;org.apache.cayenne.plugins&lt;/groupId&gt;
+    &lt;artifactId&gt;cayenne-maven-plugin&lt;/artifactId&gt;
+    &lt;version&gt;3.1.3-SNAPSHOT&lt;/version&gt;
+
+    &lt;!--
+        There's an intermittent problem when using Maven/cgen in Eclipse with  m2eclipse plugin that
+        requires placing "configuration" section at the plugin level, instead of execution
+        level.
+    --&gt;
+    &lt;configuration&gt;
+            &lt;map&gt;${project.basedir}/src/main/resources/my.map.xml&lt;/map&gt;
+            &lt;destDir&gt;${project.basedir}/src/main/java&lt;/destDir&gt;
+            &lt;superPkg&gt;org.example.model.auto&lt;/superPkg&gt;
+    &lt;/configuration&gt;
+
+    &lt;executions&gt;
+        &lt;execution&gt;
+            &lt;goals&gt;
+                &lt;goal&gt;cgen&lt;/goal&gt;
+            &lt;/goals&gt;
+        &lt;/execution&gt;
+    &lt;/executions&gt;
+&lt;/plugin&gt;</code></pre> 
+      </div> 
+     </div> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="cdbgen"><a class="anchor" href="#cdbgen"></a>cdbgen</h5> 
+     <div class="paragraph"> 
+      <p><code>cdbgen</code> is a <code>cayenne-maven-plugin</code> goal that drops and/or generates tables in a database on Cayenne DataMap. By default, it is bound to the pre-integration-test phase.</p> 
+     </div> 
+     <table id="cdbgenTable" class="tableblock frame-all grid-all spread table table-bordered"> 
+      <caption class="title">
+       Table 3. cdbgen required parameters
+      </caption> 
+      <colgroup> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 66.6668%;"> 
+      </colgroup> 
+      <thead> 
+       <tr> 
+        <th class="tableblock halign-left valign-top">Name</th> 
+        <th class="tableblock halign-left valign-top">Type</th> 
+        <th class="tableblock halign-left valign-top">Description</th> 
+       </tr> 
+      </thead> 
+      <tbody> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">map</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">File</p></td> 
+        <td class="tableblock halign-left valign-top">
+         <div>
+          <div class="paragraph"> 
+           <p>DataMap XML file which serves as a source of metadata for class generation. E.g.</p> 
+          </div> 
+          <div class="listingblock"> 
+           <div class="content"> 
+            <pre class="highlight"><code>${project.basedir}/src/main/resources/my.map.xml</code></pre> 
+           </div> 
+          </div>
+         </div></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">driver</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A class of JDBC driver to use for the target database.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">url</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">JDBC connection URL of a target database.</p></td> 
+       </tr> 
+      </tbody> 
+     </table> 
+     <table id="cdbgenOptionl" class="tableblock frame-all grid-all spread table table-bordered"> 
+      <caption class="title">
+       Table 4. cdbgen optional parameters
+      </caption> 
+      <colgroup> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 16.6666%;"> 
+       <col style="width: 66.6668%;"> 
+      </colgroup> 
+      <thead> 
+       <tr> 
+        <th class="tableblock halign-left valign-top">Name</th> 
+        <th class="tableblock halign-left valign-top">Type</th> 
+        <th class="tableblock halign-left valign-top">Description</th> 
+       </tr> 
+      </thead> 
+      <tbody> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">adapter</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Java class name implementing org.apache.cayenne.dba.DbAdapter. While this attribute is optional (a generic JdbcAdapter is used if not set), it is highly recommended to specify correct target adapter.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">createFK</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether cdbgen should create foreign key constraints. Default is "true".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">createPK</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether cdbgen should create Cayenne-specific auto PK objects. Default is "true".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">createTables</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether cdbgen should create new tables. Default is "true".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">dropPK</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether cdbgen should drop Cayenne primary key support objects. Default is "false".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">dropTables</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether cdbgen should drop the tables before attempting to create new ones. Default is "false".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">password</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Database user password.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">username</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Database user name.</p></td> 
+       </tr> 
+      </tbody> 
+     </table> 
+     <div class="paragraph"> 
+      <p>Example - creating a DB schema on a local HSQLDB database:</p> 
+     </div> 
+     <div class="listingblock"> 
+      <div class="content"> 
+       <pre class="highlight"><code class="language-xml xml" data-lang="xml">&lt;plugin&gt;
+    &lt;groupId&gt;org.apache.cayenne.plugins&lt;/groupId&gt;
+    &lt;artifactId&gt;maven-cayenne-plugin&lt;/artifactId&gt;
+    &lt;version&gt;3.1.3-SNAPSHOT&lt;/version&gt;
+
+    &lt;executions&gt;
+        &lt;execution&gt;
+            &lt;configuration&gt;
+                &lt;map&gt;${project.basedir}/src/main/resources/my.map.xml&lt;/map&gt;
+                &lt;url&gt;jdbc:hsqldb:hsql://localhost/testdb&lt;/url&gt;
+                &lt;adapter&gt;org.apache.cayenne.dba.hsqldb.HSQLDBAdapter&lt;/adapter&gt;
+                &lt;driver&gt;org.hsqldb.jdbcDriver&lt;/driver&gt;
+                &lt;username&gt;sa&lt;/username&gt;
+            &lt;/configuration&gt;
+            &lt;goals&gt;
+                &lt;goal&gt;cdbgen&lt;/goal&gt;
+            &lt;/goals&gt;
+        &lt;/execution&gt;
+    &lt;/executions&gt;
+&lt;/plugin&gt;</code></pre> 
+      </div> 
+     </div> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="mavenCdbimort"><a class="anchor" href="#mavenCdbimort"></a>cdbimport</h5> 
+     <div class="paragraph"> 
+      <p><code>cdbimport</code> is a <code>maven-cayenne-plugin</code> goal that generates a DataMap based on an existing database schema. By default, it is bound to the generate-sources phase. This allows you to generate your DataMap prior to building your project, which may be necessary if you are also using the cgen task.</p> 
+     </div> 
+     <table id="cdbimportTable" class="tableblock frame-all grid-all spread table table-bordered"> 
+      <caption class="title">
+       Table 5. cdbimport parameters
+      </caption> 
+      <colgroup> 
+       <col style="width: 20%;"> 
+       <col style="width: 10%;"> 
+       <col style="width: 20%;"> 
+       <col style="width: 50%;"> 
+      </colgroup> 
+      <thead> 
+       <tr> 
+        <th class="tableblock halign-left valign-top">Name</th> 
+        <th class="tableblock halign-left valign-top">Type</th> 
+        <th class="tableblock halign-left valign-top">Required</th> 
+        <th class="tableblock halign-left valign-top">Description</th> 
+       </tr> 
+      </thead> 
+      <tbody> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">map</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">File</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Yes</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">DataMap XML file which is the destination of the schema import. Can be an existing file. If this file does not exist, it is created when cdbimport is executed. E.g. <code>${project.basedir}/src/main/resources/my.map.xml</code>. If "overwrite" is true (the default), an existing DataMap will be used as a template for the new imported DataMap, i.e. all its entities will be cleared and recreated, but its common settings, such as default Java package, will be preserved (unless changed explicitly in the plugin configuration).</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">driver</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Yes</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A class of JDBC driver to use for the target database.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">url</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Yes</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">JDBC connection URL of a target database.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">adapter</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">A Java class name implementing org.apache.cayenne.dba.DbAdapter. This attribute is optional. If not specified, AutoAdapter is used, which will attempt to guess the DB type.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">importProcedures</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether stored procedures should be imported from the database. Default is false.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">meaningfulPk</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether primary keys should be mapped as attributes of the ObjEntity. Default is false.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">namingStrategy</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">The naming strategy used for mapping database names to object entity names. Default is <code>org.apache.cayenne.map.naming.SmartNamingStrategy</code>.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">overwriteExisting</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">boolean</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Indicates whether existing DB and object entities should be overwritten. This is an all-or-nothing setting. If you need finer granularity, use the CayenneModeler. Default is "true".</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">password</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Database user password.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">procedurePattern</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Pattern to match stored procedure names against for import. Default is to match all stored procedures. This value is only meaningful if importProcedures is true.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">schemaName</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Database schema to import tables/stored procedures from.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">tablePattern</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Pattern to match table names against for import. Default is to match all tables.</p></td> 
+       </tr> 
+       <tr> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">username</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">String</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">No</p></td> 
+        <td class="tableblock halign-left valign-middle"><p class="tableblock">Database user name.</p></td> 
+       </tr> 
+      </tbody> 
+     </table> 
+     <div class="paragraph"> 
+      <p>Example - loading a DB schema from a local HSQLDB database (essentially a reverse operation compared to the cdbgen example above) :</p> 
+     </div> 
+     <div class="listingblock"> 
+      <div class="content"> 
+       <pre class="highlight"><code class="language-XML XML" data-lang="XML">&lt;plugin&gt;
+    &lt;groupId&gt;org.apache.cayenne.plugins&lt;/groupId&gt;
+    &lt;artifactId&gt;cayenne-maven-plugin&lt;/artifactId&gt;
+    &lt;version&gt;3.1.3-SNAPSHOT&lt;/version&gt;
+
+    &lt;executions&gt;
+        &lt;execution&gt;
+            &lt;configuration&gt;
+                &lt;map&gt;${project.basedir}/src/main/resources/my.map.xml&lt;/map&gt;
+                &lt;url&gt;jdbc:mysql://127.0.0.1/mydb&lt;/url&gt;
+                &lt;adapter&gt;org.apache.cayenne.dba.hsqldb.HSQLDBAdapter&lt;/adapter&gt;
+                &lt;driver&gt;com.mysql.jdbc.Driver&lt;/driver&gt;
+                &lt;username&gt;sa&lt;/username&gt;
+            &lt;/configuration&gt;
+            &lt;goals&gt;
+                &lt;goal&gt;cdbimport&lt;/goal&gt;
+            &lt;/goals&gt;
+        &lt;/execution&gt;
+    &lt;/executions&gt;
+&lt;/plugin&gt;</code></pre> 
+      </div> 
+     </div> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="ant-projects"><a class="anchor" href="#ant-projects"></a>Ant Projects</h4> 
+    <div class="sect4"> 
+     <h5 id="cgen-2"><a class="anchor" href="#cgen-2"></a>cgen</h5> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="cdbgen-2"><a class="anchor" href="#cdbgen-2"></a>cdbgen</h5> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="cdbimport"><a class="anchor" href="#cdbimport"></a>cdbimport</h5> 
+     <div class="paragraph"> 
+      <p>This is an Ant counterpart of "cdbimport" goal of cayenne-maven-plugin described above. It has exactly the same properties. Here is a usage example:</p> 
+     </div> 
+     <div class="listingblock"> 
+      <div class="content"> 
+       <pre class="highlight"><code class="language-XML XML" data-lang="XML"> &lt;cdbimport map="${context.dir}/WEB-INF/my.map.xml"
+     driver="com.mysql.jdbc.Driver"
+     url="jdbc:mysql://127.0.0.1/mydb"
+     username="sa"/&gt;</code></pre> 
+      </div> 
+     </div> 
+    </div> 
+    <div class="sect4"> 
+     <h5 id="cdataport"><a class="anchor" href="#cdataport"></a>cdataport</h5> 
+    </div> 
+   </div> 
+  </div> 
+  <div class="sect2"> 
+   <h3 id="starting-cayenne"><a class="anchor" href="#starting-cayenne"></a>2.2. Starting Cayenne</h3> 
+   <div class="sect3"> 
+    <h4 id="starting-and-stopping-serverruntime"><a class="anchor" href="#starting-and-stopping-serverruntime"></a>Starting and Stopping ServerRuntime</h4> 
+    <div class="paragraph"> 
+     <p>In runtime Cayenne is accessed via <code>org.apache.cayenne.configuration.server.ServerRuntime</code>. ServerRuntime is created by calling a convenient builder:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">ServerRuntime runtime = new ServerRuntime("com/example/cayenne-project.xml");</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>The parameter you pass to the builder is a location of the main project file. Location is a '/'-separated path (same path separator is used on UNIX and Windows) that is resolved relative to the application classpath. The project file can be placed in the root package or in a subpackage (e.g. in the code above it is in "com/example" subpackage).</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>ServerRuntime encapsulates a single Cayenne stack. Most applications will just have one ServerRuntime using it to create as many ObjectContexts as needed, access the Dependency Injection (DI) container and work with other Cayenne features. Internally ServerRuntime is just a thin wrapper around the DI container. Detailed features of the container are discussed in <a href="#customize">"Customizing Cayenne Runtime"</a> chapter. Here we’ll just show an example of how an application might turn on external transactions:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">public class MyExtensionsModule implements Module {
+    public void configure(Binder binder) {
+        binder.bind(QueryCache.class).to(EhCacheQueryCache.class);
+    }
+}</code></pre> 
+     </div> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Module extensions = new MyExtensionsModule();
+ServerRuntime runtime = new ServerRuntime("com/example/cayenne-project.xml", extensions);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>It is a good idea to shut down the runtime when it is no longer needed, usually before the application itself is shutdown:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">runtime.shutdown();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When a runtime object has the same scope as the application, this may not be always necessary, however in some cases it is essential, and is generally considered a good practice. E.g. in a web container hot redeploy of a webapp will cause resource leaks and eventual OutOfMemoryError if the application fails to shutdown CayenneRuntime.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="merging-multiple-projects"><a class="anchor" href="#merging-multiple-projects"></a>Merging Multiple Projects</h4> 
+    <div class="paragraph"> 
+     <p>ServerRuntime requires at least one mapping project to run. But it can also take multiple projects and merge them together in a single configuration. This way different parts of a database can be mapped independenlty from each other (even by different software providers), and combined in runtime when assembling an application. Doing it is as easy as passing multiple project locations to ServerRuntime constructor:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">ServerRuntime runtime = new ServerRuntime(new String[] {
+        "com/example/cayenne-project.xml",
+        "org/foo/cayenne-library1.xml",
+        "org/foo/cayenne-library2.xml"
+    }
+);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When the projects are merged, the following rules are applied:</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p>The order of projects matters during merge. If there are two conflicting metadata objects belonging to two projects, an object from the last project takes precedence over the object from the first one. This makes possible to override pieces of metadata. This is also similar to how DI modules are merged in Cayenne.</p> </li> 
+      <li> <p>Runtime DataDomain name is set to the name of the last project in the list.</p> </li> 
+      <li> <p>Runtime DataDomain properties are the same as the properties of the last project in the list. I.e. properties are not merged to avoid invalid combinations and unexpected runtime behavior.</p> </li> 
+      <li> <p>If there are two or more DataMaps with the same name, only one DataMap is used in the merged project, the rest are discarded. Same precedence rules apply - DataMap from the project with the highest index in the project list overrides all other DataMaps with the same name.</p> </li> 
+      <li> <p>If there are two or more DataNodes with the same name, only one DataNode is used in the merged project, the rest are discarded. DataNode coming from project with the highest index in the project list is chosen per precedence rule above.</p> </li> 
+      <li> <p>There is a notion of "default" DataNode. After the merge if any DataMaps are not explicitly linked to DataNodes, their queries will be executed via a default DataNode. This makes it possible to build mapping "libraries" that are only associated with a specific database in runtime. If there’s only one DataNode in the merged project, it will be automatically chosen as default. A possible way to explicitly designate a specific node as default is to override <code>DataDomainProvider.createAndInitDataDomain()</code>.</p> </li> 
+     </ul> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="web-applications"><a class="anchor" href="#web-applications"></a>Web Applications</h4> 
+    <div class="paragraph"> 
+     <p>Web applications can use a variety of mechanisms to configure and start the "services" they need, Cayenne being one of such services. Configuration can be done within standard Servlet specification objects like Servlets, Filters, or ServletContextListeners, or can use Spring, JEE CDI, etc. This is a user’s architectural choice and Cayenne is agnostic to it and will happily work in any environment. As described above, all that is needed is to create an instance of ServerRuntime somewhere and provide the application code with means to access it. And shut it down when the application ends to avoid container leaks.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Still Cayenne includes a piece of web app configuration code that can assist in quickly setting up simple Cayenne-enabled web applications. We are talking about CayenneFilter. It is declared in web.xml:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-XML XML" data-lang="XML">&lt;web-app&gt;
+    ...
+    &lt;filter&gt;
+        &lt;filter-name&gt;cayenne-project&lt;/filter-name&gt;
+        &lt;filter-class&gt;org.apache.cayenne.configuration.web.CayenneFilter&lt;/filter-class&gt;
+    &lt;/filter&gt;
+     &lt;filter-mapping&gt;
+        &lt;filter-name&gt;cayenne-project&lt;/filter-name&gt;
+        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
+     &lt;/filter-mapping&gt;
+    ...
+ &lt;/web-app&gt;</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When started by the web container, it creates a instance of ServerRuntime and stores it in the ServletContext. Note that the name of Cayenne XML project file is derived from the "filter-name". In the example above CayenneFilter will look for an XML file "cayenne-project.xml". This can be overridden with "configuration-location" init parameter.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When the application runs, all HTTP requests matching the filter url-pattern will have access to a session-scoped ObjectContext like this:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java"> ObjectContext context = BaseContext.getThreadObjectContext();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Of course the ObjectContext scope, and other behavior of the Cayenne runtime can be customized via dependency injection. For this another filter init parameter called "extra-modules" is used. "extra-modules" is a comma or space-separated list of class names, with each class implementing Module interface. These optional custom modules are loaded after the the standard ones, which allows users to override all standard definitions.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>For those interested in the DI container contents of the runtime created by CayenneFilter, it is the same ServerRuntime as would’ve been created by other means, but with an extra <code>org.apache.cayenne.configuration.web.WebModule</code> module that provides <code>org.apache.cayenne.configuration.web.RequestHandler</code> service. This is the service to override in the custom modules if you need to provide a different ObjectContext scope, etc.</p> 
+    </div> 
+    <div class="admonitionblock note"> 
+     <table> 
+      <tbody>
+       <tr> 
+        <td class="icon"> <i class="fa fa-info-circle fa-2x" title="Note"></i> </td> 
+        <td class="content"> You should not think of CayenneFilter as the only way to start and use Cayenne in a web application. In fact CayenneFilter is entirely optional. Use it if you don’t have any special design for application service management. If you do, simply integrate Cayenne into that design. </td> 
+       </tr> 
+      </tbody>
+     </table> 
+    </div> 
+   </div> 
+  </div> 
+  <div class="sect2"> 
+   <h3 id="persistent-objects-and-objectcontext"><a class="anchor" href="#persistent-objects-and-objectcontext"></a>2.3. Persistent Objects and ObjectContext</h3> 
+   <div class="sect3"> 
+    <h4 id="objectcontext"><a class="anchor" href="#objectcontext"></a>ObjectContext</h4> 
+    <div class="paragraph"> 
+     <p>ObjectContext is an interface that users normally work with to access the database. It provides the API to execute database operations and to manage persistent objects. A context is obtained from the ServerRuntime:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">ObjectContext context = runtime.getContext();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>The call above creates a new instance of ObjectContext that can access the database via this runtime. ObjectContext is a single "work area" in Cayenne, storing persistent objects. ObjectContext guarantees that for each database row with a unique ID it will contain at most one instance of an object, thus ensuring object graph consistency between multiple selects (a feature called "uniquing"). At the same time different ObjectContexts will have independent copies of objects for each unique database row. This allows users to isolate object changes from one another by using separate ObjectContexts.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>These properties directly affect the strategies for scoping and sharing (or not sharing) ObjectContexts. Contexts that are only used to fetch objects from the database and whose objects are never modified by the application can be shared between mutliple users (and multiple threads). Contexts that store modified objects should be accessed only by a single user (e.g. a web application user might reuse a context instance between multiple web requests in the same HttpSession, thus carrying uncommitted changes to objects from request to request, until he decides to commit or rollback them). Even for a single user it might make sense to use mutliple ObjectContexts (e.g. request-scoped contexts to allow concurrent requests from the browser that change and commit objects independently).</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>ObjectContext is serializable and does not permanently hold to any of the application resources. So it does not have to be closed. If the context is not used anymore, it should simply be allowed to go out of scope and get garbage collected, just like any other Java object.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="persistent-object-and-its-lifecycle"><a class="anchor" href="#persistent-object-and-its-lifecycle"></a>Persistent Object and its Lifecycle</h4> 
+    <div class="paragraph"> 
+     <p>Cayenne can persist Java objects that implement <code>org.apache.cayenne.Persistent</code> interface. Generally persistent classes are generated from the model as described above, so users do not have to worry about superclass and property implementation details.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Persistent interface provides access to 3 persistence-related properties - objectId, persistenceState and objectContext. All 3 are initialized by Cayenne runtime framework. Application code should not attempt to change them. However it is allowed to read them, which provides valuable runtime information. E.g. ObjectId can be used for quick equality check of 2 objects, knowing persistence state would allow highlighting changed objects, etc.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Each persistent object belongs to a single ObjectContext, and can be in one of the following persistence states (as defined in <code>org.apache.cayenne.PersistenceState</code>) :</p> 
+    </div> 
+    <table id="persistenceStates" class="tableblock frame-all grid-all spread table table-bordered"> 
+     <caption class="title">
+      Table 6. Persistence States
+     </caption> 
+     <colgroup> 
+      <col style="width: 20%;"> 
+      <col style="width: 80%;"> 
+     </colgroup> 
+     <tbody> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">TRANSIENT</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is not registered with an ObjectContext and will not be persisted.</p></td> 
+      </tr> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">NEW</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is freshly registered in an ObjectContext, but has not been saved to the database yet and there is no matching database row.</p></td> 
+      </tr> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">COMMITED</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is registered in an ObjectContext, there is a row in the database corresponding to this object, and the object state corresponds to the last known state of the matching database row.</p></td> 
+      </tr> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">MODIFIED</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is registered in an ObjectContext, there is a row in the database corresponding to this object, but the object in-memory state has diverged from the last known state of the matching database row.</p></td> 
+      </tr> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">HOLLOW</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is registered in an ObjectContext, there is a row in the database corresponding to this object, but the object state is unknown. Whenever an application tries to access a property of such object, Cayenne attempts reading its values from the database and "inflate" the object, turning it to COMMITED.</p></td> 
+      </tr> 
+      <tr> 
+       <td class="tableblock halign-center valign-middle"><p class="tableblock">DELETED</p></td> 
+       <td class="tableblock halign-left valign-middle"><p class="tableblock">The object is registered in an ObjectContext and has been marked for deletion in-memory. The corresponding row in the database will get deleted upon ObjectContext commit, and the object state will be turned into TRANSIENT.</p></td> 
+      </tr> 
+     </tbody> 
+    </table> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="objectcontext-persistence-api"><a class="anchor" href="#objectcontext-persistence-api"></a>ObjectContext Persistence API</h4> 
+    <div class="paragraph"> 
+     <p>One of the first things users usually want to do with an ObjectContext is to select some objects from a database. This is done by calling "performQuery" method:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">SelectQuery query = new SelectQuery(Artist.class);
+List&lt;Artist&gt; artists = context.performQuery(query);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>We’ll discuss queries in some detail in the following chapters. The example above is self-explanatory - we create a SelectQuery that matches all Artist objects present in the database, and then call "performQuery", getting a list of Artist objects.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Some queries can be quite complex, returning multiple result sets or even updating the database. For such queries ObjectContext provides "performGenericQuery"method. While not nearly as commonly-used as "performQuery", it is nevertheless important in some situations. E.g.:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Collection&lt;Query&gt; queries = ... // multiple queries that need to be run together
+QueryChain query = new QueryChain(queries);
+
+QueryResponse response = context.performGenericQuery(query);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>An application might modify selected objects. E.g.:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Artist selectedArtist = artists.get(0);
+selectedArtist.setName("Dali");</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>The first time the object property is changed, the object’s state is automatically set to "MODIFIED" by Cayenne. Cayenne tracks all in-memory changes until a user calls "commitChanges":</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">context.commitChanges();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>At this point all in-memory changes are analyzed and a minimal set of SQL statements is issued in a single transaction to synchronize the database with the in-memory state. In our example "commitChanges" commits just one object, but generally it can be any number of objects.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>If instead of commit, we wanted to reset all changed objects to the previously committed state, we’d call <code>rollbackChanges</code> instead:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">context.rollbackChanges();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>"newObject" method call creates a persistent object and sets its state to "NEW":</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Artist newArtist = context.newObject(Artist.class);
+newArtist.setName("Picasso");</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>It will only exist in memory until "commitChanges" is issued. On commit Cayenne might generate a new primary key (unless a user set it explicitly, or a PK was inferred from a relationship) and issue an INSERT SQL statement to permanently store the object.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p><code>deleteObjects</code> method takes one or more Persistent objects and marks them as "DELETED":</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">context.deleteObjects(artist1);
+context.deleteObjects(artist2, artist3, artist4);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Additionally "deleteObjects" processes all delete rules modeled for the affected objects. This may result in implicitly deleting or modifying extra related objects. Same as insert and update, delete operations are sent to the database only when "commitChanges" is called. Similarly "rollbackChanges" will undo the effect of "newObject" and "deleteObjects".</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p><code>localObject</code> returns a copy of a given persistent object that is "local" to a given ObjectContext:</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Since an application often works with more than one context, "localObject" is a rather common operation. E.g. to improve performance a user might utilize a single shared context to select and cache data, and then occasionally transfer some selected objects to another context to modify and commit them:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">ObjectContext editingContext = runtime.newContext();
+Artist localArtist = editingContext.localObject(artist);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Often an appliction needs to inspect mapping metadata. This information is stored in the EntityResolver object, accessible via the ObjectContext:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">EntityResolver resolver = objectContext.getEntityResolver();</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Here we discussed the most commonly used subset of the ObjectContext API. There are other useful methods, e.g. those allowing to inspect registered objects state in bulk, etc. Check the latest JavaDocs for details.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="cayenne-helper-class"><a class="anchor" href="#cayenne-helper-class"></a>Cayenne Helper Class</h4> 
+    <div class="paragraph"> 
+     <p>There is a useful helper class called "Cayenne" (fully-qualified name <code>"org.apache.cayenne.Cayenne"</code>) that builds on ObjectContext API to provide a number of very common operations. E.g. get a primary key (most entities do not model PK as an object property) :</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">long pk = Cayenne.longPKForObject(artist);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>It also provides the reverse operation - finding an object given a known PK:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Artist artist = Cayenne.objectForPK(context, Artist.class, 34579);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>If a query is expected to return 0 or 1 object, Cayenne helper class can be used to find this object. It throws an exception if more than one object matched the query:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Artist artist = (Artist) Cayenne.objectForQuery(context, new SelectQuery(Artist.class));</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Feel free to explore Cayenne class API for other useful methods.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="objectcontext-nesting"><a class="anchor" href="#objectcontext-nesting"></a>ObjectContext Nesting</h4> 
+    <div class="paragraph"> 
+     <p>In all the examples shown so far an ObjectContext would directly connect to a database to select data or synchronize its state (either via commit or rollback). However another context can be used in all these scenarios instead of a database. This concept is called ObjectContext "nesting". Nesting is a parent/child relationship between two contexts, where child is a nested context and selects or commits its objects via a parent.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Nesting is useful to create isolated object editing areas (child contexts) that need to all be committed to an intermediate in-memory store (parent context), or rolled back without affecting changes already recorded in the parent. Think cascading GUI dialogs, or parallel AJAX requests coming to the same session.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>In theory Cayenne supports any number of nesting levels, however applications should generally stay with one or two, as deep hierarchies will most certainly degrade the performance of the deeply nested child contexts. This is due to the fact that each context in a nesting chain has to update its own objects during most operations.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Cayenne ROP is an extreme case of nesting when a child context is located in a separate JVM and communicates with its parent via a web service. ROP is discussed in details in the following chapters. Here we concentrate on the same-VM nesting.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>To create a nested context, use an instance of ServerRuntime, passing it the desired parent:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">ObjectContext parent = runtime.getContext();
+ObjectContext nested = runtime.getContext((DataChannel) parent);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>From here a nested context operates just like a regular context (you can perform queries, create and delete objects, etc.). The only difference is that commit and rollback operations can either be limited to synchronization with the parent, or cascade all the way to the database:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">// merges nested context changes into the parent context
+nested.commitChangesToParent();
+
+// regular 'commitChanges' cascades commit through the chain
+// of parent contexts all the way to the database
+nested.commitChanges();</code></pre> 
+     </div> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">// unrolls all local changes, getting context in a state identical to parent
+nested.rollbackChangesLocally();
+
+// regular 'rollbackChanges' cascades rollback through the chain of contexts
+// all the way to the topmost parent
+nested.rollbackChanges();</code></pre> 
+     </div> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="generic-persistent-objects"><a class="anchor" href="#generic-persistent-objects"></a>Generic Persistent Objects</h4> 
+    <div class="paragraph"> 
+     <p>As described in the CayenneModeler chapter, Cayenne supports mapping of completely generic classes to specific entities. Although for conveniece most applications should stick with entity-specific class mappings, the generic feature offers some interesting possibilities, such as creating mappings completely on the fly in a running application, etc.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Generic objects are first class citizens in Cayenne, and all common persistent operations apply to them as well. There are some pecularities however, described below.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>When creating a new generic object, either cast your ObjectContext to DataContext (that provides "newObject(String)" API), or provide your object with an explicit ObjectId:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">DataObject generic = ((DataContext) context).newObject("GenericEntity");</code></pre> 
+     </div> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">DataObject generic = new CayenneDataObject();
+generic.setObjectId(new ObjectId("GenericEntity"));
+context.registerNewObject(generic);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>SelectQuery for generic object should be created passing entity name String in constructor, instead of a Java class:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">SelectQuery query = new SelectQuery("GenericEntity");</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Use DataObject API to access and modify properties of a generic object:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">String name = (String) generic.readProperty("name");
+generic.writeProperty("name", "New Name");</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>This is how an application can obtain entity name of a generic object:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">String entityName = generic.getObjectId().getEntityName();</code></pre> 
+     </div> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="transactions"><a class="anchor" href="#transactions"></a>Transactions</h4> 
+    <div class="paragraph"> 
+     <p>Considering how much attention is given to managing transactions in most other ORMs, transactions have been conspicuously absent from the ObjectContext discussion till now. The reason is that transactions are seamless in Cayenne in all but a few special cases. ObjectContext is an in-memory container of objects that is disconnected from the database, except when it needs to run an operation. So it does not care about any surrounding transaction scope. Sure enough all database operations are transactional, so when an application does a commit, all SQL execution is wrapped in a database transaction. But this is done behind the scenes and is rarely a concern to the application code.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Two cases where transactions need to be taken into consideration are container-managed and application-managed transactions.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>If you are using an EJB container (or some other JTA environment), you’ll likely need to switch Cayenne runtime into "external transactions mode". This is either done in the Modeler (check DataDomain &gt; 'Container-Managed Transactions' checkbox), or in the code:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">runtime.getDataDomain().setUsingExternalTransactions(true);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>In this case Cayenne assumes that JDBC Connections obtained by runtime whenever that might happen are all coming from a transactional DataSource managed by the container. In this case Cayenne does not attempt to commit or rollback the connections, leaving it up to the container to do that when appropriate.</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>In the second scenario, an application might need to define its own transaction scope that spans more than one Cayenne operation. E.g. two sequential commits that need to be rolled back together in case of failure. This can be done with an explicit thread-bound transaction that surrounds a set of operations. Application is responsible for committing or rolling it back:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">Transaction tx = runtime.getDataDomain().createTransaction();
+Transaction.bindThreadTransaction(tx);
+
+try {
+    // commit one or more contexts
+    context1.commitChanges();
+    context2.commitChanges();
+    ....
+    // after changing some objects in context1, commit again
+    context1.commitChnages();
+    ....
+    // if no failures, commit
+    tx.commit();
+}
+catch (Exception ex) {
+    tx.setRollbackOnly();
+}
+finally {
+    Transaction.bindThreadTransaction(null);
+
+    if (tx.getStatus() == Transaction.STATUS_MARKED_ROLLEDBACK) {
+        try {
+           tx.rollback();
+        }
+        catch (Exception rollbackEx) {
+        }
+    }
+}</code></pre> 
+     </div> 
+    </div> 
+   </div> 
+  </div> 
+  <div class="sect2"> 
+   <h3 id="expressions"><a class="anchor" href="#expressions"></a>2.4. Expressions</h3> 
+   <div class="sect3"> 
+    <h4 id="expressions-overview"><a class="anchor" href="#expressions-overview"></a>Expressions Overview</h4> 
+    <div class="paragraph"> 
+     <p>Cayenne provides a simple yet powerful object-based expression language. The most common use of expressions are to build qualifiers and orderings of queries that are later converted to SQL by Cayenne and to evaluate in-memory against specific objects (to access certain values in the object graph or to perform in-memory object filtering and sorting). Cayenne provides API to build expressions in the code and a parser to create expressions from strings.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="path-expressions"><a class="anchor" href="#path-expressions"></a>Path Expressions</h4> 
+    <div class="paragraph"> 
+     <p>Before discussing how to build expressions, it is important to understand one group of expressions widely used in Cayenne - path expressions. There are two types of path expressions - object and database, used for navigating graphs of connected objects or joined DB tables respectively. Object paths are much more commonly used, as after all Cayenne is supposed to provide a degree of isolation of the object model from the database. However database paths are helpful in certain situations. General structure of path expressions is the following:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre> [db:]segment[+][.segment[+]...]</pre> 
+     </div> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><code>db:</code> is an optional prefix indicating that the following path is a DB path. Otherwise it is an object path.</p> </li> 
+      <li> <p><code>segment</code> is a name of a property (relationship or attribute in Cayenne terms) in the path. Path must have at least one segment; segments are separated by dot (".").</p> </li> 
+      <li> <p><code>+</code> An "OUTER JOIN" path component. Currently "+" only has effect when translated to SQL as OUTER JOIN. When evaluating expressions in memory, it is ignored.</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>An object path expression represents a chain of property names rooted in a certain (unspecified during expression creation) object and "navigating" to its related value. E.g. a path expression <code>artist.name</code> might be a property path starting from a Painting object, pointing to the related Artist object, and then to its name attribute. A few more examples:</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><code>name</code> - can be used to navigate (read) the "name" property of a Person (or any other type of object that has a "name" property).</p> </li> 
+      <li> <p><code>artist.exhibits.closingDate</code> - can be used to navigate to a closing date of any of the exhibits of a Painting’s Artist object.</p> </li> 
+      <li> <p><code>artist.exhibits+.closingDate</code> - same as the previous example, but when translated into SQL, an OUTER JOIN will be used for "exhibits".</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Similarly a database path expression is a dot-separated path through DB table joins and columns. In Cayenne joins are mapped as DbRelationships with some symbolic names (the closest concept to DbRelationship name in the DB world is a named foreign key constraint. But DbRelationship names are usually chosen arbitrarily, without regard to constraints naming or even constraints presence). A database path therefore might look like this - <code>db:dbrelationshipX.dbrelationshipY.COLUMN_Z</code>. More specific examples:</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><code>db:NAME</code> - can be used to navigate to the value of "NAME" column of some unspecified table.</p> </li> 
+      <li> <p><code>db:artist.artistExhibits.exhibit.CLOSING_DATE</code> - can be used to match a closing date of any of the exhibits of a related artist record.</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Cayenne supports "aliases" in path Expressions. E.g. the same expression can be written using explicit path or an alias:</p> 
+    </div> 
+    <div class="ulist"> 
+     <ul> 
+      <li> <p><code>artist.exhibits.closingDate</code> - full path</p> </li> 
+      <li> <p><code>e.closingDate</code> - alias <code>e</code> is used for <code>artist.exhibits</code>.</p> </li> 
+     </ul> 
+    </div> 
+    <div class="paragraph"> 
+     <p>SelectQuery using the second form of the path expression must be made aware of the alias via <code>SelectQuery.aliasPathSplits(..)</code>, otherwise an Exception will be thrown. The main use of aliases is to allow users to control how SQL joins are generated if the same path is encountered more than once in any given Expression. Each alias for any given path would result in a separate join. Without aliases, a single join will be used for a group of matching paths.</p> 
+    </div> 
+   </div> 
+   <div class="sect3"> 
+    <h4 id="creating-expressions-from-strings"><a class="anchor" href="#creating-expressions-from-strings"></a>Creating Expressions from Strings</h4> 
+    <div class="paragraph"> 
+     <p>While in most cases users are likely to rely on API from the following section for expression creation, we’ll start by showing String expressions, as this will help to understand the semantics. A Cayenne expression can be represented as a String, which can be converted to an expression object using <code>ExpressionFactory.exp</code> static method. Here is an example:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">String expString = "name like 'A%' and price &lt; 1000";
+Expression exp = ExpressionFactory.exp(expString);</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>This particular expression may be used to match Paintings whose names that start with "A" and whose price is less than $1000. While this example is pretty self-explanatory, there are a few points worth mentioning. "name" and "price" here are object paths discussed earlier. As always, paths themselves are not attached to a specific root entity and can be applied to any entity that has similarly named attributes or relationships. So when we are saying that this expression "may be used to match Paintings", we are implying that there may be other entities, for which this expression is valid. Now the expression details…​</p> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Character constants that are not paths or numeric values should be enclosed in single or double quotes. Two of the expressions below are equivalent:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">name = 'ABC'
+
+// double quotes are escaped inside Java Strings of course
+name = \"ABC\"</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Case sensitivity. Expression operators are case sensitive and are usually lowercase. Complex words follow the Java camel-case style:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">// valid
+name likeIgnoreCase 'A%'
+
+// invalid - will throw a parse exception
+name LIKEIGNORECASE 'A%'</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Grouping with parenthesis:</p> 
+    </div> 
+    <div class="listingblock"> 
+     <div class="content"> 
+      <pre class="highlight"><code class="language-java java" data-lang="java">value = (price + 250.00) * 3</code></pre> 
+     </div> 
+    </div> 
+    <div class="paragraph"> 
+     <p>Path prefixes. Object expressions are unquoted strings, optionally prefixed by <code>obj:</code> (usually they are not prefixed at all actually). Database expressio

<TRUNCATED>

Mime
View raw message