Always room for more and better
The Java™ Persistence API (JPA) was first introduced in Java Platform Enterprise Edition (Java EE) 5 as part of the Enterprise JavaBean™ (EJB) 3.0 family of specifications Since that time, JPA 1.0 has proven to be a very popular persistence framework. Even though this first specification was very complete and functional, there is always room for improvement. The next iteration of the JPA specification (JPA 2.0) is currently under development via JSR 317.
The JPA expert group is working hard to finalize the JPA 2.0 specification. Public Final Draft #2 of the specification was recently made available, and the final JPA 2.0 specification is planned to be available by November 16, 2009.
A single Comment Line column cannot do justice to all the new features in the JPA 2.0 specification. Instead, brief introductions will be made via this article followed by information to help you go experience these features firsthand via Apache’s OpenJPA project.
As I mentioned, the first JPA specification was functionally complete. Many of the new features introduced in JPA 2.0 -- especially in the areas of object/relational (O/R) mapping and domain modeling -- are for "edge" cases; scenarios that exist in the industry due to legacy applications and schemas, but are not the normal, every day type of scenarios. Other major new features, like the Criteria API and the integration with the Bean Validation framework, just add to JPA’s arsenal of persistence capabilities.
So, let’s take a closer look at each of these:
- O/R mapping and domain modeling
- Several new additions to JPA 2.0 were in the area of O/R mapping and domain modeling. The definition and usage of embeddables was expanded to include collections of embeddables, nested embeddables, and embeddables containing relationships to other entities. Of course, the Java Persistence Query Language (JPQL) was also updated to support these embeddable enhancements.
- The Access type (@Access) was also expanded in JPA 2.0 to enable specification on a per-persistent type basis, or even on individual attributes. These changes provide more flexibility when architecting your entity definitions and associated applications that use these entities.
- JPA 2.0 provides for enhanced map support for ElementCollections, in addition to the existing OneToMany, ManyToOne, and ManyToMany relationships. Related to this enhanced map collection support are the associated annotations for MapKeyColumn, MapKeyClass, and MapKeyJoinColumn. Again, JPQL was updated to support these new constructs.
- Another new feature of JPA 2.0 related to domain modeling is the definition of derived identities. This enables the ID of an entity to be derived from another entity, which provides for parent-to-dependent relationships. The JPA 2.0 specification devotes several pages (section 2.4) to derived identity scenarios.
- JPA runtime
Each of the essential JPA APIs needed to be updated for JPA 2.0: EntityManagerFactory, EntityManager, and Query. Happily, these updates were all made in an upwardly compatible fashion, allowing for a much smoother migration from both an application perspective and a JPA provider’s perspective. Many of these API updates are related to gaining access to key new JPA 2.0 features, such as the Metamodel and Criteria APIs (more on these coming up), but there are a few other run time features worth mentioning:
- One of the new features of JPA 2.0 is the introduction of a pessimistic LockManager. JPA 1.0 only defined optimistic locking semantics, but true enterprise level applications at times require more stringent pessimistic locking capabilities. Application of pessimistic lock semantics can be specified via the LockModeType on individual EntityManager or Query methods. (Apache OpenJPA had previously provided the openjpa.LockManager property and the ReadLockMode hint, which can be used to further configure pessimistic lock semantics within OpenJPA.)
- Another new runtime update for JPA 2.0 provides access to a JPA provider’s Level2 cache, if a cache is available. JPA 2.0 still does not require a JPA provider to actually provide a Level2 cache, nor does the specification define how to plug in or utilize a Level2 cache. But if a cache is available, this new API and associated annotations will help with managing the contents of the Level2 cache. (By the way, Apache OpenJPA does provide a Level2 cache plug-in with third party cache implementations available.)
- Metamodel and Criteria APIs
One of the features creating the most buzz around JPA 2.0 is the combination of the Metamodel and Criteria APIs. The Criteria API enables a programmatic construction of queries using an object-based query graph. The Criteria API provides a dynamic alternative to JPQL: instead of creating and manipulating JPQL queries using string-based classes and methods, you can now use the Criteria API to programmatically create and manipulate queries.
But what really makes the Criteria API powerful is the type-safety aspect of the API. The type-safety feature will provide for much earlier error detection at compile time, rather than at run time with the standard JPQL constructs. The Metamodel API and interfaces are the backbone of the type-safety feature. The Metamodel API can be used to dynamically access the metamodel information for a persistence unit. These metamodel constructs can be generated statically or dynamically.
- Bean validation
The support for bean validation is another key feature of JPA 2.0, which extends to both the container-managed and application-managed environments. Bean validation can be used to provide optional entity validation, based on the annotation and XML constraints, validation groups, and group sequences defined by the JSR-303 specification. The existing JPA lifecycle events for PrePersist, PreUpdate, and PreRemove provide the hook points into the bean validation framework. The JPA provider is also responsible for providing a TraversableResolver to ensure that fetch plans for attributes and entity relationships for managed objects are observed.
How do you get to experience all of these cool new features of JPA 2.0? One open source development effort of the JPA 2.0 specification is the Apache OpenJPA project, which provides the basis for the WebSphere Application Server JPA solution.
As you can see from the OpenJPA 2.0 roadmap, the development effort has been active since December 2008. Several iterations and milestones have been defined and delivered. Nightly drivers are also available if you want the absolute latest-and-greatest. These nightly 2.0 drivers are fully regression tested, so you should feel comfortable with the stability of these drivers for development purposes. If you want even more assurance, Milestone drivers have also been produced. The latest Milestone driver is based off an Early Access version of the JPA 2.0 specification. Once the JPA 2.0 Proposed Final Draft #2 becomes publicly available, then another Milestone driver will be produced. Watch the OpenJPA home page or News page for updates on availability.
What can I do with these early OpenJPA 2.0 drivers?
Anything that is documented on the Apache OpenJPA Roadmap is fair game for development purposes. With every integration of new function, corresponding JUnit tests and associated documentation updates are also being integrated. Hopefully, between the specification, the OpenJPA implementation, the test cases, and the documentation, there will be enough background information to get your feet wet with this new JPA 2.0 functionality.
If you have been following the JPA 2.0 expert group, then you know that this is a moving target. The JPA expert group is continuing to modify the specification and associated APIs on a weekly basis, although the rate of change is slowing greatly with the final version due on the horizon. Of course, because of these nearly continuous changes, be aware that the OpenJPA implementation may not be completely in sync with the expert group proceedings and that differences can occur until the final specification is delivered from the JPA expert group.
As you experiment with this new JPA 2.0 functionality, the OpenJPA community would love to hear from you. Whether you find potential bugs or misinterpretations of the spec, or whether you have other feedback on the drivers, it would be positive and beneficial to hear about your exploits on either of our User or Dev mailing lists . Naturally, if you find explicit bugs, please don’t hesitate to create JIRA Issues . We want this OpenJPA 2.0 release to be as complete as possible, so early customer feedback is encouraged and desired.
Performance and database support
Performance is always a concern, especially when so many new features are being introduced into a stable runtime base like OpenJPA 1.2.x. Rest assured that we are continually monitoring the performance aspects of these new features and how they affect the existing JPA 1.0 features. Not only do we not want to introduce any performance regressions, but one of our goals is also to improve the performance of the existing code base. Of course, we are also measuring and monitoring the performance of the new features as well.
We are also interested in constantly improving and expanding our database support. Of course, from an IBM perspective, we are looking for the best support for IBM DB2®, Informix®, Derby, and even solidDB®. OpenJPA also provides support for Oracle and Microsoft® SQL Server. Some recent commits have also improved the support of PostgreSQL and MySQL. The OpenJPA documentation outlines the breadth of database support. We continually run our complete OpenJPA 2.0 test bucket against these major databases to ensure proper coverage with all of the new features.
Hopefully, this article has piqued your interest in JPA 2.0 and the associated Apache OpenJPA 2.0 implementation. The entire JPA 2.0 effort is a work-in-progress, but the specification is firming up with November as the target for the final version, and a fully compliant release of OpenJPA 2.0 should be available shortly after that.
- JSR 220: Enterprise JavaBeans 3.0 specification
- JSR 317: Java Persistence 2.0 specification
- JSR 303: Bean Validation specification
- Apache OpenJPA
- JPA 2.0 Road Map
- OpenJPA 2.0.0 Early Access 2
- OpenJPA news and events
- Running and Writing Tests with OpenJPA
- OpenJPA Documentation
- Project Mailing Lists
- Reporting OpenJPA bugs
- Using OpenJPA with IBM solidDB V6.3 on WebSphere Application Server V7
- IBM developerWorks WebSphere
Get products and technologies
Dig deeper into WebSphere on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Keep up with the best and latest technical info to help you tackle your development challenges.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.