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
Newsletter
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
  • Database Bottlenecks Nobody Talks About: Optimizing SQL Queries Beyond Indexing
  • Seeding Postgres When Your Schema Has Foreign-Key Cycles

Trending

  • How to Migrate From LangChainGo to Genkit Go
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • AI Agents and Frontier Models: Insights From The Neuron

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Jul. 21, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Database Bottlenecks Nobody Talks About: Optimizing SQL Queries Beyond Indexing
  • Seeding Postgres When Your Schema Has Foreign-Key Cycles

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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