Java MCQ: What does the @Inheritance annotation in JPA specify?
Answer:
Explanation:
The @Inheritance annotation in JPA specifies the inheritance strategy to be used for entity classes that inherit from a common superclass. JPA supports different inheritance strategies, such as SINGLE_TABLE, JOINED, and TABLE_PER_CLASS, which determine how the entity data is mapped to the underlying database tables.
Here’s an example of using @Inheritance:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {
@Id
private Long id;
private String name;
// Getters and setters
}
@Entity
public class Manager extends Employee {
private String department;
// Getters and setters
}
In this example, the Employee class is the base class, and the Manager class extends it. The @Inheritance(strategy = InheritanceType.JOINED) annotation specifies that the inheritance strategy is JOINED, meaning that each subclass will have its own table, and the primary key will be shared across the tables.
The @Inheritance annotation is essential for managing inheritance hierarchies in JPA and determining how data is stored in the database.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html