Image Blog 4 JPA Best Practices
July 19, 2016

4 JPA Best Practices for Better Application Performance

Java Application Development
Enterprise Development

While many application performance issues can be out of a developers hands, there are ways, especially in JPA implementations, to make sure your code is using databases efficiently. In our article today, we're looking at a handful of ways to streamline application performance in JPA.

Improve Performance in Your JPA Implementation

In our article today, I'll cover four best practices that can help improve performance in JPA implementations:

Avoid Making Too Many Queries

By default, most JPA implementations will fetch data in 1-1 relationships eagerly. On the other hand, it fetches data from one-to-many and many-to-many relationships lazily. It makes sense, but depending on the needs of your application, this might not be the most performant configuration. Lazy strategies breed queries; to reduce the number of DB queries JPA makes, while keeping the sensible default lazy loading strategy, consider the following best practices.

Named Entity Graphs

JPA entity graphs specify a collection of entities that will be fetched from the database in a query independent way. Let's define one that fetches Books at the same time as Authors:

Named Entity Graph snippet

Use this graph as a hint to the entity manager to fetch all this data in one query.

Entity Graph request

JPQL Joins

An alternative is to use a join clause, which is similar to a SQL join, as follows:

Entity Manager Fetch Example

Use Bulk Operations

Updating and deleting entities one by one is very inefficient when executing over large sets of entities. CriteriaUpdate and CriteriaDelete allows you to construct an update statement that updates multiple entities at once via a bulk operation.

CriteriaDelete works similarly to CriteriaUpdate.

Using Bulk Operations in JPA

Don't Fetch More Data Than You Need

Take into consideration how much data you actually need.


Use pagination directly in the database. Specify how many results you want to fetch at once, including the offset into the result set for the rows you're interested in.

Using pagination in JPA

Column Select

Only retrieve the required columns you need in your select operations. If you only need Author IDs, only get Author IDs!

JPA Column Select Example

Process Data in the Database

Using stored procedures can result in noticeable performance increases. Here's a stored procedure example: Define a @NamedStoredProcedureQuery on a relevant entity class:

Using Stored Procedures in JPA

To execute the stored procedure through JPA, provide the input parameters and observe the output parameter values after the execution. Executing a stored procedure in JPA

Additional Resources

This isn't the first resource on the internet that talks about how to use JPA the right way, and that's because it's easy to make these mistakes! All the more is it important to constantly monitor JPA environments for the best possible performance. If you're looking for additional resources on JPA implementation and performance, be sure to check out these resources:

Want to save time on Java development? Check out JRebel, a JVM plugin that skips redeploys and boosts productivity. Try JRebel free for 14 days!

Try JRebel for Free