Introduction
Object inheritance can be mapped in 3 ways in JPA/Hibernate.
- SINGLE_TABLE
- TABLE_PER_CLASS
- JOINED
SINGLE_TABLE - @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
All Inheritance Java Classes are stored in one big database table.
Default @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
pros
- Faster query, since no joining of tables
cons
- Data Integrity. Cannot uphold NOT NULL constraints, etc, but can be enforced via a CHECK of TRIGGER. Bean Validation can improve, but does not help if either integrate directly with the database.
- Works when smaller set of different inhereted class, but single table grows
TABLE_PER_CLASS - @MappedSuperclass
pros
- Can enforce NOT NULL constraints, etc
cons
- You rarely need to joind inhereted java class, so no real cons
JOINED - @Inheritance(strategy = InheritanceType.JOINED)
https://vladmihalcea.com/the-best-way-to-use-entity-inheritance-with-jpa-and-hibernate/
Do no use, no efficient SQL.
Summary
Use TABLE_PER_CLASS with @MappedSuperclass.
No comments:
Post a Comment