Pratik Patel on Enterprise JPA, Fetch Groups and Spring 2.5
JPA extension features like FetchGroups offered by Apache OpenJPA framework can give enterprise Java developers more flexibility in retrieving the data mapped to a graph of Java objects. Pratik Patel discussed the JPA design techniques for developing real-world Java applications using JPA API and Spring 2.5 framework. He did a presentation at the recent No Fluff Just Stuff (NFJS) Central Ohio Software Symposium on Enterprise JPA and Spring support for the persistence API.
Pratik also talked about organizing multiple persistence units (PU) and performance tuning techniques in Java applications that are using JPA API for persistence requirements. Pratik mentioned that EJB 3.0 Entities (formerly known as Entity Beans) use JPA which is based on the design concept of "POJO persistence". He said it's necessary in Java applications to use vendor specific extensions like "Fetch Groups" to do real JPA work.
Spring framework 2.5 version has good JPA integration with support for managing multiple persistence units and it makes JPA configuration more flexible by off-loading some of the JPA configuration details (like data source) from persistence.xml to Spring configuration files. The developers can use the extension points when using Spring to configure JPA for organizing a large code base into smaller modules. Also, Spring Test module has great support for unit and integration testing of JPA classes (for example AbstractJpaTests). AbstractJpaTests class uses a shadowing classloader to enhance classes on the fly. It wraps each test method around a transaction and also provides a helper for declaring base Application Contexts. Spring JPA module provides (LocalContainerEntityManagerFactoryBean) for unit testing JPA logic outside the container.
Speaking of Locking, Pratik said that JPA specification defaults to optimistic locking. Developers can use pessimistic locking but they will need to define the lock manager using EntityManager's lock() or find() methods. JPA also uses a "version" based approach to track any updates to the data in memory. This requires that there is a @Version attribute defined in the persistent classes and a Version column created in the database table. Avoid Pessimistic Locking since the pessimistic locks affect the throughput and it's difficult to recover from bad locks.
Pratik demonstrated JPA API usage and performance tuning using @FetchGroup and @FetchPlan annotations provided by Apache OpenJPA framework. He also talked about unit and integration testing of fetching logic and recommended to test the fetch groups code to prevent any last minute surprises when the application code is promoted to production environment.
Regarding the JPA best practices, he asked the developers to consider the techniques like using an in-memory database (like HSQL DB) to eliminate mock DAO implementations. The trade-off is that connecting to an in-memory database will take little more unit test time, but there will be less code and development effort. Other best practices include evaluating generated SQL which gives a good insight into the SQL queries generated by the ORM tool. Another thing to consider is the DDL generation since all JPA implementations allow generation of DDL which can be used for rapid development. Other items to consider when using JPA API for persistence are:
- Ordering in collections (JPA 2.0 version will introduce a new annotation called @OrderColumn which defines the column to use as the collating column).
- Flush early and often as subsequent queries within the same transaction will NOT see NON-flushed data
- Enums - speed vs. readability in the database.
In another JPA related presentation, Pratik discussed the JPA Query Language (JPAQL) which has features like natural Object Oriented (OO) querying and bulk updates and deletes. JPAQL is similar to other query languages like JDOQL and HQL, but it's limited compared to HQL. For lazy relationships, FETCH JOIN expression comes handy to fetch the associated details along with the master table data. He also talked about the new features in JPA 2.0 specification like:
- "Criteria" query
- Collections for basic types (Integer and String)
- Expanded Map support
- More JPAQL expressions.
- Better Locking support.
Tom Gilb & Kai Gilb Jan 26, 2015