如何使用Java注释配置Hibernate继承映射?在Annotations中使用继承有什么好处?
答案 0 :(得分:52)
3种可能的类型:
<强> 1。每个类层次结构策略的单个表:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name="planetype",
discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue("Plane")
public class Plane { ... }
@Entity
@DiscriminatorValue("A320")
public class A320 extends Plane { ... }
<hibernate-mapping>
<subclass name="DomesticCat" extends="Cat" discriminator-value="D">
<property name="name" type="string"/>
</subclass>
<强> 2。加入子类策略:
数据库表
CREATE TABLE SUPER_TABLE(
id_col number primary key,
sup_Name varchar2(20));
CREATE TABLE SUB_TABLE(
SUP_ID primary key,
sub_name varchar2(20),
constraint SUB_TABLE_fk foreign key (sup_Id) references super_table(id_col));
@Entity
@Table(name= "SUPER_TABLE")
@Inheritance(strategy= InheritanceType.JOINED)
public class TestSuperClass {
@Id
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="SEQ_GEN")
@SequenceGenerator(
name="SEQ_GEN",
sequenceName="hibernate_sequence"
)
@Column(name ="id_col")
private long idcol;
@Column(name ="sup_name")
private String supName;
@Entity
@Table(name="SUB_TABLE")
@PrimaryKeyJoinColumn(name="SUP_ID")
<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" column="CCTYPE"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
public class TestSubClass extends TestSuperClass{
private String sub_name;
}
测试模块
TestSubClass sub = new TestSubClass("sub1");
sub.setSupName("supersuper"); session1.save(sub);
SQL Generated
Hibernate: insert into SUPER_TABLE (sup_name, id_col) values (?, ?)
Hibernate: insert into SUB_TABLE (sub_name, SUP_ID) values (?, ?)
第3。每个具体课程策略表:
create table CREDIT_CARD( payment_id number primary key, amount
number, creditCardType varchar2(2) );
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Payment {
@Id
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="SEQ_GEN")
@SequenceGenerator(
name="SEQ_GEN",
sequenceName="hibernate_sequence"
)
@Column(name = "payment_id")
private long id;
private double amount;
@Entity
@Table(name="CREDIT_CARD")
public class CreditCardPayment extends Payment {
private String creditCardType;
<class name="Payment">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="sequence"/>
</id>
<property name="amount" column="AMOUNT"/>
...
<union-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<property name="creditCardType" column="CCTYPE"/>
...
</union-subclass>
测试模块
CreditCardPayment credit = new CreditCardPayment("C",1.0);
session1.save(credit);
SQL Generated
Hibernate: insert into CREDIT_CARD (amount, creditCardType, payment_id) values (?, ?, ?)
还有我们在我们的应用程序中使用的@MappedSuperClass。
答案 1 :(得分:7)
这是一个非常笼统的问题,但我建议您查看以下资源:
但是你问题的基本答案是你应该使用@Inheritance
注释,如下所示:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Flight implements Serializable {
...
}
答案 2 :(得分:0)
我建议在@MappedSuperclass上使用@Inheritance注释。过去我们遇到过一些问题,@ MappedSuperclass足够灵活,可以满足我们日常的维护需求。