Java.Hibernate.Medium.What is default inheritance strategy ?

Short Answer

The default inheritance strategy in JPA/Hibernate is SINGLE_TABLE.


🔎 Detailed Explanation

  • If you define an inheritance hierarchy with @Inheritance or even without specifying the strategy, JPA assumes:
@Inheritance // equivalent to @Inheritance(strategy = InheritanceType.SINGLE_TABLE)

This means:

  • All classes in the hierarchy share one table in the database.
  • A discriminator column (by default named DTYPE) identifies which subclass each row represents.

If you don’t set @DiscriminatorColumn, JPA auto-generates one named DTYPE.

Example:

@Entity
@Inheritance // same as @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Animal { ... }

@Entity
public class Dog extends Animal { ... }

@Entity
public class Cat extends Animal { ... }

This setup will create a single table (e.g., Animal) with columns for all fields in Animal, Dog, and Cat.

📌 Key Takeaways

✅ If you don’t explicitly specify an inheritance strategy, SINGLE_TABLE is used by default.
✅ Hibernate uses a discriminator column (DTYPE) to track which subclass each row belongs to.
✅ Always choose a strategy explicitly if you want a different mapping → JOINED or TABLE_PER_CLASS.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.