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 throws 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.