Reputation: 1452
I have three classes having class hierarchy as
ParentClass.java
having commons properties used for both ChildClass1 and ChildClass2.
ChildClass1 extends ParentClass
having properties used for this ChildClass1 + it also use some of the common properties from parent class
ChildClass2 extends ParentClass
having properties used for this ChildClass2 + it also use some of the common properties from parent class
This all properties are available into table with two columns
**Key value** Type
---------------------------------------
propertyKey1 propertyValue1 Child1
propertyKey2 propertyValue2 Child1
propertyKey3 propertyValue3 Child2
propertyKey4 propertyValue4 Child2
propertyKey5 propertyValue5 CommonPorperty
.. .. ..
propertyKeyn propertyValuen ..
Now I am not sure that how to load them from hibernate inheritance ?
Apology for silly question...
Thanks in advance
Upvotes: 2
Views: 93
Reputation: 1000
You need to put an additional column 'Discriminator' to inform Hibernate which instance should be loaded when you're working on same table with multiple types. See example:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "PARENT_TABLE")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class ParentEntity implements java.io.Serializable {
private long id;
private String commonValue1;
@Id
@Column(name = "ID", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "Common_Value1")
public String getCommonValue1(){
return commonValue1;
}
public void setCommonValue1(String commonValue1){
this.commonValue1 = commonValue1;
}
}
@Entity
@DiscriminatorValue("1")
public class ChildEntity1 extends ParentEntity {
private String child1Value;
@Column(name = "Child1_Value")
public String getChild1Value(){
return child1Value;
}
public void setChild1Value(String child1Value){
this.child1Value = child1Value;
}
}
@Entity
@DiscriminatorValue("2")
public class ChildEntity2 extends ParentEntity {
private String child2Value;
@Column(name = "Child2_Value")
public String getChild2Value(){
return child2Value;
}
public void setChild2Value(String child2Value){
this.child2Value = child2Value;
}
}
Upvotes: 1