You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Bobby Lawrence <ro...@jlab.org> on 2005/04/20 20:56:01 UTC

another Oracle issue

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?

-- 
----------------------------
Bobby Lawrence
MIS Application Developer

Jefferson Lab (www.jlab.org)

 Email: robertl@jlab.org
Office: (757) 269-5818
 Pager: (757) 584-5818
----------------------------





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: another Oracle issue

Posted by Armin Waibel <ar...@apache.org>.
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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: another Oracle issue

Posted by Bobby Lawrence <ro...@jlab.org>.
Sorry - I had that wrong-
Database:
ORGANIZATION_ID ORGANIZATION_NAME
--------------- -----------------
           692 JLAB


 From the Person class:
org.jlab.mis.apps.mics.valueobjects.Person personsName='Bobby Lawrence' 
and organizationId='693'




Bobby Lawrence wrote:

> Well -
> I have traced this issue back to the sequences...
> It seems that OJB is inserting an Organization object with the even 
> id, but the Person object gets the odd id:
> In the database:
>
> ORGANIZATION_ID ORGANIZATION_NAME
> --------------- -----------------
>            692 JLAB
>
>
> From the Person class:
>
> org.jlab.mis.apps.mics.valueobjects.Person personsName='Bobby 
> Lawrence' and organizationId='677'
>
> I don't use any extents, so why is OJB calling my sequence twice?  
> Once for the object reference insert and once for the Person's 
> Organization object?
> --Bobby
>
> 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?
>>
>

-- 
----------------------------
Bobby Lawrence
MIS Application Developer

Jefferson Lab (www.jlab.org)

 Email: robertl@jlab.org
Office: (757) 269-5818
 Pager: (757) 584-5818
----------------------------





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: another Oracle issue

Posted by Bobby Lawrence <ro...@jlab.org>.
Well -
I have traced this issue back to the sequences...
It seems that OJB is inserting an Organization object with the even id, 
but the Person object gets the odd id:
In the database:

ORGANIZATION_ID ORGANIZATION_NAME
--------------- -----------------
            692 JLAB


 From the Person class:

org.jlab.mis.apps.mics.valueobjects.Person personsName='Bobby Lawrence' and organizationId='677'

I don't use any extents, so why is OJB calling my sequence twice?  Once 
for the object reference insert and once for the Person's Organization 
object?
--Bobby

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?
>

-- 
----------------------------
Bobby Lawrence
MIS Application Developer

Jefferson Lab (www.jlab.org)

 Email: robertl@jlab.org
Office: (757) 269-5818
 Pager: (757) 584-5818
----------------------------





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org