Introduced two years ago as part of Java EE 5, the Java Persistence API provides a POJO persistence model for object-relational mapping. It was developed by the EJB 3.0 software Expert Group as part of JSR-220.
Persistence consists of three areas:
- The API, defined in the javax.persistence package.
- The Java Persistence Query Language (JPQL).
- Object/relational metadata.
Whilst the JPQL significantly improved working with persistent Java objects, JPQL queries are still specified as strings. Thus whilst the queries operate over strongly-typed Java objects, they themselves are weakly typed. Building up queries in this way can be error prone, and requires specific IDE support for validation, auto completion and refactoring.
JPA 2.0, which is being developed under JSR-317 for inclusion in Java EE 6, aims to address this through the introduction of a new criteria API that offers a non-string-based approach for building queries. Expert Group lead Linda DeMichiel has published a blog entry describing the current draft of the API:
"Loosely speaking, a QueryDefinition object can be thought of as a set of nodes corresponding to the semantic constructs of the query:
- Domain objects, which correspond to the range variables and other identification variables of the JPQL FROM clause
- Where clause predicates, which comprise one or more conditional expression objects
- Select clauses, which comprise one or more "select item" objects
- Order-by and group-by items
- Subqueries
and so on..."
Whilst the proposal as it stands appears to be a good step forward from the existing JPA mechanism a number of people, amongst them Gavin King, have argued that the type safety of it could and should be further improved. King, whose Hibernate O/R tool was amongst those that pioneered the use of a type-safe criteria API and was a significant influence on EJB3, has now submitted his own proposal to the Expert Group. His proposal exploits the javax.annotation.Processor introduced in Java 6 to allow a compiler plugin to build a metamodel type for each persistent class in the application. King has written two blogs posts (one, two) which describe his approach in more detail and he and his team are currently working on a prototype annotation processor for use with javac.
The Expert Group is looking at the King proposal seriously and is considering it as a replacement for the current review draft. DeMichiel told us:
"Discussion has largely focused on ensuring that this API leads to a better experience for developers, both for static queries (where the type-safe aspects should certainly shine), and for dynamic queries. We are also looking at the metamodel generation aspects."
She added that the Expert Group is very keen to hear any feedback from the development community. Please feed any comments in to jsr-317-pdr-feedback at sun dot com.