Find below an example of mixed inheritance strategy:
Base.java
Code:
package com.hibernate.entity;
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name = "TYPE",
discriminatorType = DiscriminatorType.STRING
)
@DiscriminatorValue("BASE")
public class Base {
@Id
@GeneratedValue
private Long id;
private String baseProperty;
public String getBaseProperty() {
return baseProperty;
}
public void setBaseProperty(String baseProperty) {
this.baseProperty = baseProperty;
}
}
Sub1.javaCode:
package com.hibernate.entity;
import javax.persistence.*;
@Entity
@DiscriminatorValue("SUB1")
public class Sub1 extends Base {
private String sub1Property;
public String getSub1Property() {
return sub1Property;
}
public void setSub1Property(String sub1Property) {
this.sub1Property = sub1Property;
}
}
Sub2.javaCode:
package com.hibernate.entity;
import javax.persistence.*;
@Entity
@DiscriminatorValue("SUB2")
@SecondaryTable(name = "SUB2_EXT")
public class Sub2 extends Base {
private String sub2Property;
@Column(table = "SUB2_EXT")
private String joinedProperty;
public String getSub2Property() {
return sub2Property;
}
public void setSub2Property(String sub2Property) {
this.sub2Property = sub2Property;
}
public String getJoinedProperty() {
return joinedProperty;
}
public void setJoinedProperty(String joinedProperty) {
this.joinedProperty = joinedProperty;
}
}
This generates the following schema:
Quote:
DEBUG SchemaUpdate - create table Base (TYPE varchar(31) not null, id bigint generated by default as identity (start with 1), baseProperty varchar(255), sub1Property varchar(255), sub2Property varchar(255), primary key (id))
DEBUG SchemaUpdate - create table SUB2_EXT (joinedProperty varchar(255), id bigint not null, primary key (id))
DEBUG SchemaUpdate - alter table SUB2_EXT add constraint FKB1FFE6549102BE0F foreign key (id) references Base