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 Bruno CROS <br...@gmail.com> on 2006/02/15 13:11:05 UTC

SequenceManagerNextValImpl buildIdentity

  Hi all,

I just experienced a strange behaviour  of  SequenceManagerNextValIpl (I
guess) following migration from OJB 1.0.1 to 1.0.4.

To build an object, i need the value of the id before writing it to the
database So i use

Identity oid = broker.serviceIdentity().getIdentity(objectNewInstanciated);
//(broker is different from tx one)

and read pk value with:

oid.getPrimaryKeyValues()[0]

At this point, my id is 1001 and object is not in database. Note that my
database sequence is 975 !!

And after tx.commit(), surprise, object is created  with the right id, 975.

I don't understand anything at this. Why the id is not the final id (as it
's using OJB 1.0.1)?
Any setting idea ?

OJB 1.0.4
pk setting : autoincrement="true" sequence-name="SEQ_CALCUL"
primarykey="true" access="anonymous"

SequenceManager  : autonaming="false"

Thanks

Re: SequenceManagerNextValImpl buildIdentity

Posted by Armin Waibel <ar...@apache.org>.
Hi Bruno,


Bruno CROS wrote:
>   Hi all,
> 
> I just experienced a strange behaviour  of  SequenceManagerNextValIpl (I
> guess) following migration from OJB 1.0.1 to 1.0.4.
> 
> To build an object, i need the value of the id before writing it to the
> database So i use
> 
> Identity oid = broker.serviceIdentity().getIdentity(objectNewInstanciated);
> //(broker is different from tx one)
> 
> and read pk value with:
> 
> oid.getPrimaryKeyValues()[0]
> 
> At this point, my id is 1001 and object is not in database. Note that my
> database sequence is 975 !!
> 
> And after tx.commit(), surprise, object is created  with the right id, 975.
> 
> I don't understand anything at this. Why the id is not the final id (as it
> 's using OJB 1.0.1)?
> Any setting idea ?
>

sorry, this is a "documentation bug". I forget completely to update the 
corresponding section
http://db.apache.org/ojb/docu/guides/sequencemanager.html#Force+computation+of+unique+values 


In 1.0.4 we introduce two different states of Identity, transient 
Identity for (new) non-persistent objects and persistent Identity for 
persistent objects.
One reason was to solve a problem with table-per-subclass inheritance
http://www.mail-archive.com/ojb-dev%40db.apache.org/msg02226.html

Further on OJB no longer assign the PK values to the persistent object 
on create of Identity objects (using IdentityFactory). The reason to 
introduce this was to "harmonize" the behavior of OJB when using 
different sequence generation strategies.
E.g. when using database based identity columns (e.g. mysql) it's not 
possible to lookup the generated PK value before the object is written 
to database, thus it's not possible to create a persistent Identity 
before the object is persistent.

The first value you get (1001) is the id of the transient Identity 
object used by OJB. If you check the PK field of your object at this 
time you will still get 'null'. This is the change compared with older 
versions.
While insert the object, OJB assign the PK field with the real generated 
sequence id. Using SequenceManagerNextValImpl it's done immediately 
before the object was written to database, using 
SequenceManagerNativeImpl it's done immediately after the object was 
written to DB.

Otherwise your code depends on the used sequence generation strategy.

If you need the "old" behavior it should be possible to force assignment 
of PK value when using
Identity oid = new Identity(broker, obj);
or use TransactionExt.flush() to force object insert and then build the 
Identity or read the PK field.

regards,
Armin




> OJB 1.0.4
> pk setting : autoincrement="true" sequence-name="SEQ_CALCUL"
> primarykey="true" access="anonymous"
> 
> SequenceManager  : autonaming="false"
> 
> Thanks
> 

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