【发布时间】:2010-09-21 13:29:21
【问题描述】:
我在 JPA 中有一个使用联合策略的多级继承模型
@Entity
@Table(name="PARTY")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="PARTY_TYPE",discriminatorType=DiscriminatorType.STRING)
public class Party implements Serializable{
...
}
@Entity
@Table(name="PARTY_ORG")
@DiscriminatorValue(value="PARTY_ORG") // value in party table's PARTY_TYPE column that dictates an Org.
@PrimaryKeyJoinColumn(name="AFF_PARTY_ORG_ID")
//how children clients and orgs willmap to us.
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="ORG_TYPE_CD")
public class PartyOrg extends Party implements Serializable{
....
}
..继续使用子类
但是每当我尝试插入时,底层 DB2 数据库都会抛出 FK 约束错误,因为 PartyOrg 的 PK 也是指向 Party 的 FK。
这意味着 JPA 在尝试持久化 PartyOrg 之前必须持久化并刷新 Party。 (我已经用 manul SQl 验证了插入没有 Party 的 PartyOrg 是错误的原因。先插入 Party,然后是 PartyOrg(具有相同的 ID)工作正常。
所以
我如何告诉 JPA 首先保留顶级类以尊重子类/表的 FK 约束。
【问题讨论】:
标签: orm inheritance jpa