Skip to content

Files

Latest commit

 

History

History

HibernateSpringBootOneToManyBidirectional

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

The Best Way To Map The @OneToMany Bidirectional Association

SlideShare presentation can be found here.

Description: This application is a proof of concept of how is correct to implement the bidirectional @OneToMany association from the performance perspective.

Key points:

  • always cascade from parent to child
  • use mappedBy on the parent
  • use orphanRemoval on parent in order to remove children without references
  • use helper methods on parent to keep both sides of the association in sync
  • use lazy fetching on both side of the association
  • as entities identifiers, use assigned identifiers (business key, natural key (@NaturalId)) and/or database-generated identifiers and override (on child-side) properly the equals() and hashCode() methods as here
  • if toString() need to be overridden, then pay attention to involve only the basic attributes fetched when the entity is loaded from the database

Note: Pay attention to remove operations, especially to removing child entities. The CascadeType.REMOVE and orphanRemoval=true may produce too many queries. In such scenarios, relying on bulk operations is most of the time the best way to go for deletions.


If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.