Take the situation listed in this question:
Mapping multi-Level inheritance in Hibernate
How would this mapping be done with Annotations rather than an hbm file?
Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.
JPA annotations are used in mapping java objects to the database tables, columns etc.
There are three inheritance mapping strategies defined in the hibernate: Table Per Hierarchy. Table Per Concrete class. Table Per Subclass.
What specifically are you having trouble with? Mapping class hierarchy via joined subclasses is pretty straightforward:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class A implements Serializable { ... }
@Entity
public class B extends A { ... }
@Entity
@PrimaryKeyJoinColumn(name="A_ID")
public class C extends A { ... }
@Entity
@PrimaryKeyJoinColumn(name="B_ID")
public class D extends B { ... }
Update (based on Michal's comment).
In case you do want to use discriminators (and you should have a good reason to do so), it's possible to do so by mixing table-per-class-hierarchy strategy with secondary tables:
@Entity
@Table(name="A_table")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="entity_type")
@DiscriminatorValue("A")
public class A implements Serializable { ... }
@Entity
@SecondaryTable(name="B_table")
public class B extends A { ... }
@Entity
@SecondaryTable(name="C_table", pkJoinColumns={
@PrimaryKeyJoinColumn(name="A_ID", referencedColumnName="ID")
))
public class C extends A { ... }
@Entity
@SecondaryTable(name="D_table", pkJoinColumns={
@PrimaryKeyJoinColumn(name="B_ID", referencedColumnName="ID")
))
public class D extends B { ... }
The downside to this approach is you'll have to explicitly specify the table for each property mapped:
public class D extends B {
@Column(table="D_table")
private String someProperty;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With