RSS Feed for This PostCurrent Article

Alternative to Hibernate – ORM using Apache Cayenne

Cayenne is a Java object relational mapping (ORM) framework. In other words, it is a tool for Java developers who need to talk to a database (or many databases). Rather than hardcoding SQL statements through Java code, Cayenne allows a programmer to work only with Java objects abstracted from the database. Here are just a few benefits of the Cayenne approach to persistence:

  • Portability between almost any JDBC database without changing a single line of code in your application.
  • No knowledge of SQL is required (while it still can be helpful).
  • Code which validates any data committed to the database is easy to write and foolproof in operation. This might be as simple as ensuring passwords have enough characters, or a complex check on the validity of a set of accounting operations in a general ledger transaction. This allows you to move common error checking code out of the GUI layer and provides valuable protection against programming mistakes.
  • Caching in order to make your application faster and avoid repeated hits on the database for the same data.
  • Automatic faulting (lazy loading) of relationships, but easily supports prefetching of related data for improved performance when needed.
  • Paging which reduces bandwidth and query times by only loading the contents of objects when they are actually needed. The classic example of paging, which differs from faulting, is when a query returns 97 records, and you want to display 10 at-a-time to the user. With paging, only the first 10 records are fully loaded. Cayenne will automatically load only the page of records as they are requested.
  • Configurable optimistic locking to ensure data integrity and prevent unexpected data issues when another tool has changed the database behind the scenes (such as a maintainer updating a record in the database while a Cayenne-based application had the same record loaded to make changes).
  • A GUI-based database/schema modeler to simplify learning Cayenne. The modeler saves to XML-based files, which can be hand-edited if needed.

Cayenne can also work in three tier (ROP) mode where multiple clients connect to the data source not via JDBC but through a remote Cayenne controlled service. This gives much greater control over centralized validation, caching and a seamless persistence of objects from the server through to the clients. The clients might themselves be web servers delivering a distributed load balancing web farm or a rich GUI client such as a desktop Swing/SWT application. Recent work has been done on tying Cocoa/Objective-C clients into this environment.


Trackback URL


RSS Feed for This Post1 Comment(s)

  1. Tony | Dec 15, 2009 | Reply

    Frankly, I found Cayanne to be a bit outmoded by current ORM frameworks. For simple RestultSet-bean binding Apache DBUtils is far simpler and easier to use. For more complex applications I found the lack of of annotations a deal breaker. I don’t want my persistence objects to have to extend a Cayenne class. That needlessly ties my application to this framework. Why not support JPA annotations? There are a few other issues, but I’ll leave it at that.

Sorry, comments for this entry are closed at this time.