InfoQ

News

Pratik Patel on Enterprise JPA, Fetch Groups and Spring 2.5

Posted by Srini Penchikala on Aug 25, 2008

Community
Java
Topics
Persistence
Tags
Spring ,
JPA ,
Open JPA

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.

No comments

Watch Thread Reply

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.