blob: 92457aed61fbd3ae61d30b4d549e544c5f87d14f [file] [log] [blame]
---
# 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: "Guide to 4.0 Features"
description: "This guide highlights the new features and changes introduced in Apache Cayenne 4.0"
cayenneVersion: "4.0"
docsMenuTitle: "Upgrade Guide"
weight: 50
---
<div class="sect1">
<h2 id="guide-to-4-0-features"><a class="anchor" href="#guide-to-4-0-features"></a>1. Guide to 4.0 Features</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This guide highlights the new features and changes introduced in Apache Cayenne 4.0. For a full list of changes consult RELEASE-NOTES.txt included in Cayenne download. For release-specific upgrade instructions check UPGRADE.txt.</p>
</div>
<div class="sect2">
<h3 id="java-version"><a class="anchor" href="#java-version"></a>1.1. Java Version</h3>
<div class="paragraph">
<p>Minimum required JDK version is 1.7 or newer. Cayenne 4.0 is fully tested with Java 1.7, 1.8.</p>
</div>
<div class="paragraph">
<p>The examples below often use Java 8 syntax. But those same examples should work without lambdas just as well.</p>
</div>
</div>
<div class="sect2">
<h3 id="cayenne-configuration"><a class="anchor" href="#cayenne-configuration"></a>1.2. Cayenne Configuration</h3>
<div class="sect3">
<h4 id="serverruntimebuilder"><a class="anchor" href="#serverruntimebuilder"></a>ServerRuntimeBuilder</h4>
<div class="paragraph">
<p>Cayenne 3.1 introduced dependency injection and ServerRuntime. 4.0 provides a very convenient utility to create a custom runtime with various extensions. This reduces the code needed to integrate Cayenne in your environment to just a few lines and no boilerplate. E.g.:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">ServerRuntime runtime = ServerRuntime.builder("myproject")
.addConfigs("cayenne-project1.xml", "cayenne-project2.xml")
.addModule(binder -&gt; binder.bind(JdbcEventLogger.class).toInstance(myLogger))
.dataSource(myDataSource)
.build();</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="mapping-free-serverruntime"><a class="anchor" href="#mapping-free-serverruntime"></a>Mapping-free ServerRuntime</h4>
<div class="paragraph">
<p>ServerRuntime can now be started without any ORM mapping at all. This is useful in situations when Cayenne is used as a stack to execute raw SQL, in unit tests, etc.</p>
</div>
</div>
<div class="sect3">
<h4 id="di-container-decorators"><a class="anchor" href="#di-container-decorators"></a>DI Container Decorators</h4>
<div class="paragraph">
<p>In addition to overriding services in DI container, Cayenne now allows to supply decorators. True to the "smallest-footprint" DI philosophy, decorator approach is very simple and does not require proxies or class enhancement. Just implement the decorated interface and provide a constructor that takes a delegate instance being decorated:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">public class MyInterfaceDecorator implements MyInterface {
private MyInterface delegate;
public MockInterface1_Decorator3(@Inject MyInterface delegate) {
this.delegate = delegate;
}
@Override
public String getName() {
return "&lt;" + delegate.getName() + "&gt;";
}
}
Module module = binder -&gt;
binder.decorate(MyInterface.class).before(MyInterfaceDecorator.class);</code></pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="framework-api"><a class="anchor" href="#framework-api"></a>1.3. Framework API</h3>
<div class="sect3">
<h4 id="fluent-query-api"><a class="anchor" href="#fluent-query-api"></a>Fluent Query API</h4>
<div class="paragraph">
<p>Fluent Query API is one of the most exciting new features in Cayenne 4.0. This syntax is "chainable" so you can write query assembly and execution code on one line. The most useful fluent queries are <code>ObjectSelect</code>, <code>SQLSelect</code> and <code>SelectById</code>:</p>
</div>
<div class="sect4">
<h5 id="objectselect"><a class="anchor" href="#objectselect"></a>ObjectSelect</h5>
<div class="paragraph">
<p>A "chainable" analog of SelectQuery.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">Artist a = ObjectSelect
.query(Artist.class)
.where(Artist.ARTIST_NAME.eq("Picasso"))
.selectOne(context);</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="columnselect"><a class="anchor" href="#columnselect"></a>ColumnSelect</h5>
<div class="paragraph">
<p>This query allows you directly access individual properties of Objects and use functions (including aggregate) via type-safe API.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">List&lt;String&gt; names = ObjectSelect
.columnQuery(Artist.class, Artist.ARTIST_NAME)
.where(Artist.ARTIST_NAME.length().gt(6))
.select(context);</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="sqlselect"><a class="anchor" href="#sqlselect"></a>SQLSelect</h5>
<div class="paragraph">
<p>A "chainable" analog of <code>SQLTemplate</code> used specifically to run selecting raw SQL:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">List&lt;Long&gt; ids = SQLSelect
.scalarQuery(Long.class, "SELECT ARTIST_ID FROM ARTIST ORDER BY ARTIST_ID")
.select(context);</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="selectbyid"><a class="anchor" href="#selectbyid"></a>SelectById</h5>
<div class="paragraph">
<p>There’s really no good analog of <code>SelectById</code> in Cayenne prior to 4.0. Previously available <code>ObjectIdQuery</code> didn’t support half of the features of <code>SelectById</code> (e.g. caching consistent with other queries, prefetches, etc.) :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">Artist a = SelectById
.query(Artist.class, 3)
.useLocalCache("g1")
.selectOne(context)</code></pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="objectcontext"><a class="anchor" href="#objectcontext"></a>ObjectContext</h4>
<div class="sect4">
<h5 id="callback-based-object-iterator"><a class="anchor" href="#callback-based-object-iterator"></a>Callback-based Object Iterator</h5>
<div class="paragraph">
<p>ObjectContext now features a simpler way to iterate over large result sets, based on callback interface that can be implemented with a lambda:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">SelectQuery&lt;Artist&gt; q = new SelectQuery&lt;Artist&gt;(Artist.class);
context.iterate(q, (Artist a) -&gt; {
// do something with the object...
...
});</code></pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="generics-in-expressions-and-queries"><a class="anchor" href="#generics-in-expressions-and-queries"></a>Generics in Expressions and Queries</h4>
<div class="paragraph">
<p>We finished the work of "genericizing" Cayenne APIs started in 3.1. Now all APIs dealing with persistent objects (Expressions, Queries, ObjectContext, etc.) support generics of Persistent object type or attribute property type.</p>
</div>
</div>
<div class="sect3">
<h4 id="property-api"><a class="anchor" href="#property-api"></a>Property API</h4>
<div class="paragraph">
<p>Persistent superclasses (_MyEntity) now contain a set of static <code>Property&lt;T&gt;</code> variables generated from the model. These metadata objects make possible to create type-safe Expressions and other query parts.</p>
</div>
</div>
<div class="sect3">
<h4 id="positional-parameter-bindings"><a class="anchor" href="#positional-parameter-bindings"></a>Positional Parameter Bindings</h4>
<div class="paragraph">
<p>Expressions and <code>SQLTemplate</code> traditionally supported binding of parameters by name as a map. Cayenne 4.0 introduces a very easy form of positional bindings. It works with the same named template format, only parameters are bound by position, left-to-right. Here we showing a more complex example with the same parameter name being used more than once in the query:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">// two distinct names, 3 positional parameters.
// "price" is set to 23, "maxPrice" - to 50
Expression e = ExpressionFactory.exp(
"price = $price or averagePrice = $price and maxPrice = $maxPrice", 23, 50);</code></pre>
</div>
</div>
<div class="paragraph">
<p>This API is supported in Expressions, SQLTemplate as well as new SQLSelect and can be used interchangeably with named parameters with a single template flavor.</p>
</div>
</div>
<div class="sect3">
<h4 id="improved-transaction-api"><a class="anchor" href="#improved-transaction-api"></a>Improved Transaction API</h4>
<div class="paragraph">
<p>Transaction factory is now setup via DI (instead of being configured in the Modeler). There’s a utility method on ServerRuntime to perform multiple operations as one transaction:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">runtime.performInTransaction(() -&gt; {
// ... do some changes
context.commitChanges();
// ... do more changes
context.commitChanges();
return true;
});</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="transparent-database-cryptography-with-cayenne-crypto-module"><a class="anchor" href="#transparent-database-cryptography-with-cayenne-crypto-module"></a>Transparent Database Cryptography with "cayenne-crypto" Module</h4>
<div class="paragraph">
<p>Cayenne includes a new module called "cayenne-crypto" that enables transparent cryptography for designated data columns. This is a pretty cool feature that allows to enable encryption/decryption of your sensitive data pretty much declaratively using your regular DB storage. Encrypted values can be stored in (VAR)BINARY and (VAR)CHAR columns. Currently "cayenne-crypto" supports AES/CBC/PKCS5Padding encryption (though other cyphers can be added). It also supports encrypted data compression. Here is an example of building a crypto DI module that can be added to ServerRuntime:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">Module cryptoExtensions = CryptoModule.extend()
.keyStore("file:///mykeystore", "keystorepassword".toCharArray(), "keyalias")
.compress()
.module();</code></pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="cayennemodeler"><a class="anchor" href="#cayennemodeler"></a>1.4. CayenneModeler</h3>
<div class="sect3">
<h4 id="improved-ui"><a class="anchor" href="#improved-ui"></a>Improved UI</h4>
<div class="paragraph">
<p>CayenneModeler features a number of UI improvements. Attributes and relationships are now edited in the same view (no need to switch between the tabs). Project tree allows to easily filter elements by type and quickly collapse the tree.</p>
</div>
</div>
<div class="sect3">
<h4 id="dropped-support-for-mapping-listeners"><a class="anchor" href="#dropped-support-for-mapping-listeners"></a>Dropped Support for Mapping Listeners</h4>
<div class="paragraph">
<p>Managing listeners in the DataMap XML model is counterproductive and confusing, so support for listeners was removed from both the XML and the Modeler. If you previously had listeners mapped in the model, annotate their callback methods, and perform listener registration in the code:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">runtime.getDataDomain().addListener(myListener);</code></pre>
</div>
</div>
<div class="paragraph">
<p>or via DI:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java java" data-lang="java">Module module = binder -&gt; ServerModule.contributeDomainListeners(myListener);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Entity callbacks on the other hand are managed in the Modeler as before.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="build-tools"><a class="anchor" href="#build-tools"></a>1.5. Build Tools</h3>
<div class="sect3">
<h4 id="cdbimport"><a class="anchor" href="#cdbimport"></a>cdbimport</h4>
<div class="paragraph">
<p><code>cdbimport</code> has evolved from an experiment to a full-featured production tool that significantly reduces (and sometimes eliminates) the need for manual maintenance of the DataMaps in CayenneModeler. Two improvements made this possible. First, smart merge algorithm will ensure that custom changes to the model are not overridden on subsequent runs of "cdbimport". Second, the mechanism for specifing DB reverse-engineering parameters, such as name filtering, is made much more powerful with many new options. E.g. we started supporting filters by catalogs and schemas, table name filters can be added per catalog/schema or at the top level, etc.</p>
</div>
</div>
<div class="sect3">
<h4 id="cgen"><a class="anchor" href="#cgen"></a>cgen</h4>
<div class="paragraph">
<p>As was mentioned above, <code>cgen</code> now includes <code>Property&lt;T&gt;</code> static variables for expression building. It is also made smarter about its defaults for "destDir" and "superPkg".</p>
</div>
</div>
<div class="sect3">
<h4 id="gradle-plugin"><a class="anchor" href="#gradle-plugin"></a>Gradle Plugin</h4>
<div class="paragraph">
<p>Cayenne now provides it’s own Gradle Plugin that allows you easily integrate <code>cdbimport</code> and <code>cgen</code> tools into your build process. Here is example of it’s usage:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-Groovy Groovy" data-lang="Groovy">buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.apache.cayenne.plugins', name: 'cayenne-gradle-plugin', version: '4.0'
}
}
apply plugin: 'org.apache.cayenne'
cayenne.defaultDataMap 'datamap.map.xml'
dependencies {
compile cayenne.dependency('server')
compile cayenne.dependency('java8')
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>