This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
We have a number of tables related to each other with foreign key constraints. We are modelling these constraints as relationships in entity beans. As an example for the problem encountered, I am providing 3 of the tables involved in the relationships. For simplicity, I have removed some of the irrelevant fields from the tables.
As can be seen, TREG_SECURITY and TREG_CPP_DOC_EXCHANGE tables have a foreign key constraint with TREG_PARTY table. And TREG_CPP_DOC_EXCHANGE has two foreign key constraints with TREG_SECURITY table.
The compound primary key of TREG_CPP_DOC_EXCHANGE ( CPP_ID, PARTY_ID, DOC_EXCHANGE_ID ) has two of the fields ( CPP_ID, PARTY_ID ) in common with the two foreign keys specified by (CPP_ID, PARTY_ID, *_SECURITY_ID). When this is modelled in entity beans as relationships, the foreign key relationships are represented as CMR-fields and the columns themselves are CMP-fields.
During the creation of CppDocExchangeBean (Entity bean of TREG_CPP_DOC_EXCHANGE), we face a problem. If the entire foreign key is part of the primary key, then we can simply use the setXXX methods in the ejbCreate() for all the columns and the CMR-fields are not set. If the entire foreign key is NOT part of the primary key, then we can use setXXX methods of the CMR-fields in ejbPostCreate().
Since a part of the foreign key forms a part of the primary key, we are unable to set the SENDER_SECURITY_ID and RECEIVER_SECURITY_ID in either ejbCreate() or ejbPostCreate().
When we try to set in ejbCreate(), we hit the following error :