Skip to content

Files

Latest commit

 

History

History

HibernateSpringBootOneToManyUnidirectional

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Why You Should Avoid Unidirectional @OneToMany And Prefer Bidirectional @OneToMany Relationship

Description: As a rule of thumb, unidirectional @OneToMany association is less efficient than the bidirectional @OneToMany or the unidirectional @ManyToOne associations. This application is a sample that exposes the DML statements generated for reads, writes and removal operations when the unidirectional @OneToMany mapping is used.

Key points:

  • regular unidirectional @OneToMany is less efficient than bidirectional @OneToMany association
  • using @OrderColumn come with some optimizations for removal operations but is still less efficient than bidirectional @OneToMany association
  • using @JoinColumn eliminates the junction table but is still less efficient than bidirectional @OneToMany association
  • using Set instead of List or bidirectional @OneToMany with @JoinColumn relationship (e.g., @ManyToOne @JoinColumn(name = "author_id", updatable = false, insertable = false)) still performs worse than bidirectional @OneToMany association

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.