How to Populate a Child-Side Parent Association via a Hibernate-Specific Proxy

DZone 's Guide to

How to Populate a Child-Side Parent Association via a Hibernate-Specific Proxy

A handy solution to avoid a common persistence performance penalty.

· Java Zone ·
Free Resource

In this article, we talk about a very handy performance optimization that can be applied in the persistence layer using Spring Data and Hibernate persistence provider.

You can fetch an entity by identifier via the Spring built-in query methods, findById() or getOne(). Behind the findById() method, Spring uses EntityManager#find(), and behind the getOne() method, Spring uses EntityManager#getReference().

Calling findById() returns the entity from the Persistence Context, the Second Level Cache, or the database (this is the strict order of attempting to find the indicated entity). Therefore, the returned entity is the same type as the declared entity mapping.

On the other hand, calling getOne() will return a Hibernate-specific proxy object. This is not the actual entity type. A Hibernate-specific proxy can be useful when a child entity can be persisted with a reference to its parent (@ManyToOne or @OneToOne lazy association). In such cases, fetching the parent entity from the database (executing the corresponding SELECT statement) is a performance penalty and merely a pointless action, because Hibernate can set the underlying foreign key value for an uninitialized proxy. 

Let’s put this statement in practice via the @ManyToOne association. This association is a common JPA association, and it maps exactly to the one-to-many table relationship.

Therefore, consider that the Author and Book entities are involved in a unidirectional lazy @ManyToOne association. In the following example, the Author entity represents the parent-side, while the Book is the child-side. The author and book tables involved in this relationship are shown in the figure below:

author and book table relationships

Consider that, in the author table, there is one author with an ID of 1. Now, let’s create a Book for this entry.

Using findById( )

Relying on findById() may result in the following code (of course, don’t use orElseThrow() in production; here, orElseThrow() is just a quick shortcut to extract the value from the returned Optional):


Calling addBookToAuthor() triggers the following SQL statements:


First, a SELECT query is triggered via findById(). This SELECT fetches the author from the database. Next, the INSERT statement saves the new book by setting the foreign key, author_id

Using getOne( )

Relying on getOne() may result in the following code:


Since Hibernate can set the underlying foreign key value of an uninitialized proxy, this code triggers a single INSERT statement:


Obviously, this is better than using findById().

The complete code is available on GitHub

If you liked this article, then you'll my book containing 150+ performance items - Spring Boot Persistence Best Practices.

Spring Boot Persistence best practices

This book helps every Spring Boot developer to squeeze the performances of the persistence layer.  

hibernate, java, jpa, performance, persistence, spring boot, spring data jpa

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}