Hi Bobby,
when using a 1:1 reference OJB always first store the referenced object and then the main object. The internal rules used by OJB when storing a object are
store all 1:1 references (if auto-update is enabled) store main object store 1:n, m:n references (if auto-update is enabled)
Did you check the generated SQL with p6spy? Or could you log the SQL with Oracle. You have enabled auto-update so the Organisation object should be stored first, then the Person object, so the FK constraint from Person to Organisation shouldn't be a problem.
regards, Armin
Bobby Lawrence wrote:
I have another issue. I have a Person object. This object has a reference to an Organization object. descriptors:
<class-descriptor class="org.jlab.mis.apps.mics.valueobjects.Person" table="users">
<field-descriptor name="id" column="user_id" jdbc-type="BIGINT" primarykey="true" autoincrement="true" sequence-name="user_seq"/>
<field-descriptor name="firstName" column="firstname" jdbc-type="VARCHAR" />
<field-descriptor name="lastName" column="lastname" jdbc-type="VARCHAR" />
<field-descriptor name="organizationId" column="organization_id" jdbc-type="BIGINT" />
<reference-descriptor name="personsOrganization" class-ref="org.jlab.mis.apps.mics.valueobjects.Organization" auto-update="object">
<foreignkey field-ref="organizationId" />
</reference-descriptor>
</class-descriptor>
<class-descriptor class="org.jlab.mis.apps.mics.valueobjects.Organization" table="organizations">
<field-descriptor name="id" column="organization_id" jdbc-type="BIGINT" primarykey="true" autoincrement="true" sequence-name="organization_seq"/>
<field-descriptor name="name" column="organization_name" jdbc-type="VARCHAR"/>
</class-descriptor>
Here is my code for adding a Person:
public void addPerson(Person person) throws DatasourceException{ PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); broker.beginTransaction(); broker.store(person); broker.commitTransaction(); } catch(Exception ex){ if(broker != null) broker.abortTransaction(); throw new DatasourceException("Could not add person", ex); } finally{ if (broker != null) broker.close(); } }
My question is this:
1) Upon an insert of a Person, the PersistenceBroker inserts an Organization first because of the auto-update="object" attribute.
I have set up FK constraints on the DB. When the primary key is not set in the object, the PB uses the SequenceManagerNextValImpl to get the next value of the primary key.
When the broker tries to then insert the person, I get a KeyViolatedException!:
org.apache.ojb.broker.KeyConstraintViolatedException: SQL failure while insert object data for class org.jlab.mis.apps.mics.valueobjects.Person, PK of the given object is [ id=2906], object was org.jlab.mis.apps.mics.valueobjects.Person personsName='Bobby Lawrence' and organizationId='677', exception message is [ORA-02291: integrity constraint (MICS.USER_ORG_FK) violated - parent key not found.
It seems that the PersistenceBroker does not set the "organizationId" in the "users" table. The MICS.USER_ORG_FK is an Oracle contraint that checks to make sure that the Organization id exists in the Organizations table.
What am I doing wrong?
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
