InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Pratik Patel on Enterprise JPA, Fetch Groups and Spring 2.5

Posted by Srini Penchikala on Aug 25, 2008

Sections
Development,
Architecture & Design
Topics
Java ,
Persistence
Tags
JPA ,
Spring ,
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.

Srini Penchikala currently works as Security Architect and has 17 yrs of experience in software product management.

No comments

Watch Thread Reply

Educational Content

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?