Skip to content

Files

Latest commit

 

History

History

HibernateSpringBootChooseOnlyOneAssociation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

How To Ensure/Validate That Only One Association Is Non-Null

Description: Consider an entity named Review. This entity defines three @ManyToOne relationships to Book, Article and Magazine. A review can be associated with either a book, a magazine or an article. To validate this constraint, we can rely on Bean Validation as in this application.

Key points:

  • rely on Bean Validation to validate that only one association is non-null
  • expose the constraint via a custom annotation (@JustOneOfMany) added at class-level to the Review entity
  • for preventing native query to break our constraint add the validation at database level as well (e.g., in MySQL add a TRIGGER)

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.