Comment lines by Kevin Sutter: An update on Java Persistence API 2.0

As the JPA Expert Group (JSR-317) nears completion of the JPA 2.0 specification, this article introduces you to some of the new concepts and features in this updated specification. It also explains how you can experience this new functionality with the Apache OpenJPA project, an open-source, robust, high performance, scalable implementation of the JPA specification. This content is part of the IBM WebSphere Developer Technical Journal.

Kevin Sutter (sutter@us.ibm.com), Senior Software Engineer, IBM

Author photoKevin Sutter is a Senior Software Engineer in the WebSphere Application Server development group. He is currently the lead architect for the Java Persistence API solution for WebSphere Application Server. Kevin is also a committer and a member of the PMC for the Apache OpenJPA project. Kevin's past roles include leading and architecting the WebSphere solutions for J2EE Connector Architecture (JCA) and WebSphere eXtremeScale (caching framework).



30 September 2009

Also available in Chinese Russian Japanese

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.


Persistence

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.


Apache OpenJPA

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.


Summary

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.

Resources

Learn

Get products and technologies

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere, Java technology
ArticleID=431144
ArticleTitle=Comment lines by Kevin Sutter: An update on Java Persistence API 2.0
publish-date=09302009