DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • Manage Hierarchical Data in MongoDB With Spring
  • The Aggregate Reference Problem
  • Building a CRUD Application With Spring and SimpleJdbcMapper

Trending

  • Give Your AI Assistant Long-Term Memory With perag
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • DZone's Article Submission Guidelines
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Populate a Child-Side Parent Association via a Hibernate-Specific Proxy

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

A handy solution to avoid a common persistence performance penalty.

By 
Anghel Leonard user avatar
Anghel Leonard
DZone Core CORE ·
Jul. 21, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

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):

Java
xxxxxxxxxx
1
12
 
1
@Transactional
2
public void addBookToAuthor() {
3
    
4
  Author author = authorRepository.findById(1L).orElseThrow();
5
    
6
  Book book = new Book();
7
  book.setIsbn("001-MJ");
8
  book.setTitle("The Canterbury Anthology");
9
  book.setAuthor(author);
10
    
11
  bookRepository.save(book);
12
}


Calling addBookToAuthor() triggers the following SQL statements:

SQL
xxxxxxxxxx
1
10
 
1
SELECT
2
  author0_.id AS id1_0_0_,
3
  author0_.age AS age2_0_0_,
4
  author0_.genre AS genre3_0_0_,
5
  author0_.name AS name4_0_0_
6
FROM author author0_
7
WHERE author0_.id = ?
8
  
9
INSERT INTO book (author_id, isbn, title)
10
  VALUES (?, ?, ?)


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:

Java
xxxxxxxxxx
1
12
 
1
@Transactional
2
public void addBookToAuthor() {
3
    
4
  Author proxy = authorRepository.getOne(1L);
5
    
6
  Book book = new Book();
7
  book.setIsbn("001-MJ");
8
  book.setTitle("The Canterbury Anthology");
9
  book.setAuthor(proxy);
10
    
11
  bookRepository.save(book);
12
}


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

SQL
x
 
1
INSERT INTO book (author_id, isbn, title)
2
  VALUES (?, ?, ?)


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.  

Spring Framework Database Relational database

Opinions expressed by DZone contributors are their own.

Related

  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • Manage Hierarchical Data in MongoDB With Spring
  • The Aggregate Reference Problem
  • Building a CRUD Application With Spring and SimpleJdbcMapper

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook