1. Overview

Apache Cayenne is an open-source library, distributed under the Apache license, providing features like a modeling tool, object-relational mapping aka ORM for local persistence operations and remoting services.

In the following sections, we’ll see how to interact with a MySQL database using Apache Cayenne ORM.

2. Maven Dependencies

To start, we just need to add the following dependencies to bring up Apache Cayenne and MySQL connector the JDBC driver together to access our intro_cayenne database:


Let’s configure Cayenne modeler plugin that will be used for designing or setting our mapping file that acts as a bridge between the database schema and the Java object:


Instead of building XML mapping file by hand (made rarely), it’s recommended to use the modeler which is a pretty advanced tool that comes with the Cayenne distribution.

It’s available for download from this archive depending on your OS or just use the cross-platform version (JAR) included as a Maven plugin up there.

The Maven Central repository host the latest versions of Apache Cayenne, his modeler, and MySQL Connector.

Next, let’s build our project with the mvn install and launch the modeler GUI with the command mvn cayenne-modeler:run to get as output this screen:


3. Setup

To make Apache Cayenne look up the correct local database, we just need to fill his configuration file with the right driver, URL, and a user in the file cayenne-project.xml located in the resources directory:

<?xml version="1.0" encoding="utf-8"?>
<domain project-version="9">
    <node name="datanode"
            <driver value="com.mysql.jdbc.Driver"/>
            <url value
            <connectionPool min="1" max="1"/>
            <login userName="root" password="root"/>

Here we can see that:

  • The local database is named intro_cayenne
  • If it’s not yet created, Cayenne will do it for us
  • We’ll connect using the username root and password root (change it according to the users registered in your database management system)

Internally, it’s the XMLPoolingDataSourceFactory responsible for loading JDBC connection information from an XML resource associated to the DataNodeDescriptor.

Be aware that these parameters are relative to the database management system and a JDBC driver because this library can support many different databases.

Each of them has an adapter available in this detailed list. Notice, that the full documentation for version 4.0 is not yet available, so we refer to the previous version here.

4. Mapping & Database Design

4.1. Modeling

Let’s now click on the “Open Project”, navigate to the project’s resources folder and chose the file cayenne-project.xml, the modeler will show this:

Capture d’écran

Here, we’ve got the choice to either create our mapping structure from an existing database or to proceed manually. This article will handle the one using the modeler and existing database to get into Cayenne and know quickly how it works.

Let’s take a look at our intro_cayenne database which has a one-to-many relationship across two tables, as an author can publish or own many articles:

  • author: id (PK) and name
  • article: id (PK), title, content, and author_id(FK)

Now let’s go to “Tools > Reengineer Database Schema“, and we’ll have all our mapping configs filled automatically. On the prompt screen just fill the data source configuration available up there in the cayenne-project.xml file and hit continue:

Capture d’écran

On the next screen, we need to check “Use Java primitive types” as follows:

Capture d’écran

We need also to ensure to put com.baeldung.apachecayenne.persistent as Java package and save it; we’ll see that the XML configuration file has been updated for its defaultPackage property to match the Java package:

Capture d’écran

In each ObjEntity we must specify the package for subclasses as shown in the following image and click the “save” icon again:


Now on “Tools > Generate Classes” menu, select “Standard Persistent Objects” as the type; and on the “Classes” tab check all classes and hit “generate”.

Let’s go back to the source code to see that our persistent objects have been generated successfully, talking about _Article.java and _Author.java.

Note that all these configurations are saved in the file datamap.map.xml also located in the resources folder.

4.2. Mapping Structure

The generated XML mapping file presents in the resource folder is using some unique tags relative to Apache Cayenne:

  • DataNode(<node>) – the model of the database, its contents all information necessary to get connected to a database (the name of the database, the driver and the user credentials)
  • DataMap(<data-map>) – it’s a container of persistent entities with their relations
  • DbAttribute(<db-attribute>) – represents a column in a database table
  • DbEntity(<db-entity>) – the model of a single database table or view, it can have DbAttributes and relationships
  • ObjEntity(<obj-entity>) – the model of a single persistent java class; made of ObjAttributes that correspond to entity class properties and ObjRelationships that are properties that have a type of another entity
  • Embeddable(<embeddable>) – the model of a Java class that acts as a property of an ObjEntity, but corresponds to multiple columns in the database
  • Procedure(<procedure>) – to register stored procedure in the database
  • Query(<query>) – the model of a query, used to mapped query in configuration file without forget that we can also do it in the code

Here are the full details.

5. Cayenne API

The only remaining step is to use the Cayenne API to do our database operations using generated classes, knowing that subclassing our persistent classes is just a best practice used for customizing the model later.

5.1. Creating an Object

Here, we just save an Author object and check later that there is only one record of this type in the database:

public void whenInsert_thenWeGetOneRecordInTheDatabase() {
    Author author = context.newObject(Author.class);


    long records = ObjectSelect.dataRowQuery(Author.class)
    assertEquals(1, records);

5.2. Reading an Object

After saving an Author, we just pick it among others via a simple query by a particular property:

public void whenInsert_andQueryByFirstName_thenWeGetTheAuthor() {
    Author author = context.newObject(Author.class);


    Author expectedAuthor = ObjectSelect.query(Author.class)

    assertEquals("Paul", expectedAuthor.getName());

5.3. Retrieving All Records of a Class

We’re going to save two authors and retrieve a collection of author objects to check that there are just these two saved:

public void whenInsert_andQueryAll_thenWeGetTwoAuthors() {
    Author firstAuthor = context.newObject(Author.class);

    Author secondAuthor = context.newObject(Author.class);


    List<Author> authors = ObjectSelect
    assertEquals(2, authors.size());

5.4. Updating an Object

The updating process is easy too, but we need first to have the desired object before modifying its properties and applying it to the database:

public void whenUpdating_thenWeGetAnUpatedeAuthor() {
    Author author = context.newObject(Author.class);

    Author expectedAuthor = ObjectSelect.query(Author.class)

    assertEquals(author.getName(), expectedAuthor.getName());

5.5. Attaching an Object

We can assign an article to an author:

public void whenAttachingToArticle_thenTheRelationIsMade() {
    Author author = context.newObject(Author.class);

    Article article = context.newObject(Article.class);
    article.setTitle("My post title");
    article.setContent("The content");


    Author expectedAuthor = ObjectSelect.query(Author.class)

    Article expectedArticle = (expectedAuthor.getArticles()).get(0);
    assertEquals(article.getTitle(), expectedArticle.getTitle());

5.6. Deleting an Object

The deletion of a saved object completely removes it from the database, thereafter we’ll see null as the result of the query:

public void whenDeleting_thenWeLostHisDetails() {
    Author author = context.newObject(Author.class);

    Author savedAuthor = ObjectSelect.query(Author.class)
    if(savedAuthor != null) {

    Author expectedAuthor = ObjectSelect.query(Author.class)

5.7. Delete All Records of a Class

It’s also possible to drop all the records of a table usingSQLTemplate, here we do this after each test method to always have a void database before each test is launched:

public void deleteAllRecords() {
    SQLTemplate deleteArticles = new SQLTemplate(
      Article.class, "delete from article");
    SQLTemplate deleteAuthors = new SQLTemplate(
      Author.class, "delete from author");


6. Conclusion

In this tutorial, we focused on using Apache Cayenne ORM to easily demonstrate how to do CRUD operations with a one-to-many relationship.

As always, the source code for this article can be found over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.