开发者

Enforce FK Constraints when JPA inserts inherited objects

I have a multilevel inheritance model in JPA that is using the joined strategy

@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{
....
}

..continues with children classes

But whenever I try to insert, the underlying DB2 databse thro开发者_运维问答ws a FK constraint error because the PK of PartyOrg is also a FK pointing to Party.

That means that JPA must persists and flush Party before it attempts to persist PartyOrg. (I have verified with manul SQl that inserting a PartyOrg without a Party is the cause of the error. Inserting Party first, then a PartyOrg (with same ID) works fine.

SO

How can I tell JPA to persist top level classes first to respect the FK constraint on children classes/tables.


Discovered the problem!

The DB2 database is using floats for PKs. When my DBA said "use floats" I never stopped to consider the mismatch between DB world and Java world. Turns out that DB2 "floats" should in turn call for Java doubles. After I converted all my keys to doubles, everything works.

I can only guess that rounding issues in the floats caused FK mismatches - but that is pure speculation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜