InfoQ

News

Pratik Patel on Enterprise JPA, Fetch Groups and Spring 2.5

Posted by Srini Penchikala on Aug 25, 2008 06:00 AM

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

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.