Apache Cayenne is an open source persistence framework for Java. With a wealth of unique and powerful features, Cayenne can address a wide range of persistence needs. Cayenne seamlessly binds one or more database schemas to Java objects, managing atomic commits and rollbacks, SQL generation, joins, sequences, and more.
Cayenne supports database reverse engineering and generation, as well as templated class generation. All of these functions can be controlled through the GUI CayenneModeler app. An entire database schema can be mapped directly to Java objects within minutes, all from the comfort of the GUI-based CayenneModeler.
Cayenne supports numerous other features, including caching, an object query syntax, relationship pre-fetching, on-demand object and relationship faulting, object inheritance, database auto-detection, and generic persistent objects. Cayenne can scale up or down to virtually any project size.
Downloaded Cayenne Modeler from https://cayenne.apache.org/download/, start it and create a Cayenne project.
Maven
<dependencies>
<dependency>
<groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-server</artifactId>
<version>5.0-M2</version>
</dependency>
</dependencies>Gradle
compile group: 'org.apache.cayenne', name: 'cayenne-server', version: '5.0-M1'// or, if Gradle plugin is used
compile cayenne.dependency('server')CayenneRuntimecayenneRuntime = CayenneRuntime.of()
.addConfig("cayenne-demo.xml")
.dataSource(CayenneDataSource
.of("jdbc:mysql://localhost:3306/cayenne_demo")
.userName("username")
.password("password")
.build())
.build();ObjectContextcontext = cayenneRuntime.newContext();
Artistpicasso = context.newObject(Artist.class);
picasso.setName("Pablo Picasso");
picasso.setDateOfBirth(LocalDate.of(1881, 10, 25));
Gallerymetropolitan = context.newObject(Gallery.class);
metropolitan.setName("Metropolitan Museum of Art");
Paintinggirl = context.newObject(Painting.class);
girl.setName("Girl Reading at a Table");
Paintingstein = context.newObject(Painting.class);
stein.setName("Gertrude Stein");
picasso.addToPaintings(girl);
picasso.addToPaintings(stein);
girl.setGallery(metropolitan);
stein.setGallery(metropolitan);
context.commitChanges();List<Painting> paintings = ObjectSelect.query(Painting.class)
.where(Painting.ARTIST.dot(Artist.DATE_OF_BIRTH).year().lt(1900))
.prefetch(Painting.ARTIST.joint())
.select(context);// this is artificial property signaling that we want to get full objectProperty<Artist> artistProperty = Property.createSelf(Artist.class);
List<Object[]> artistAndPaintingCount = ObjectSelect.columnQuery(Artist.class, artistProperty, Artist.PAINTING_ARRAY.count())
.where(Artist.ARTIST_NAME.like("a%"))
.having(Artist.PAINTING_ARRAY.count().lt(5L))
.orderBy(Artist.PAINTING_ARRAY.count().desc(), Artist.ARTIST_NAME.asc())
.select(context);
for(Object[] next : artistAndPaintingCount) {
Artistartist = (Artist)next[0];
longpaintingsCount = (Long)next[1];
System.out.println(artist.getArtistName() + " has " + paintingsCount + " painting(s)");
}// Selecting objectsList<Painting> paintings = SQLSelect
.query(Painting.class, "SELECT * FROM PAINTING WHERE PAINTING_TITLE LIKE #bind($title)")
.params("title", "painting%")
.upperColumnNames()
.localCache()
.limit(100)
.select(context);
// Selecting scalar valuesList<String> paintingNames = SQLSelect
.scalarQuery(String.class, "SELECT PAINTING_TITLE FROM PAINTING WHERE ESTIMATED_PRICE > #bind($price)")
.params("price", 100000)
.select(context);
// Insert valuesintinserted = SQLExec
.query("INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME) VALUES (#bind($id), #bind($name))")
.paramsArray(55, "Picasso")
.update(context);