Re: Ability to access primary key prior to Store of Object

Armin Waibel <[email protected]>
Newsgroups gmane.comp.jakarta.ojb.user
Message-ID <[email protected]>
Hi Gary,

Gary Bartlett wrote:
> Greetings
> 
> I am working on upgrading an application that was
> built using OJB 0.94 to release 1.0.4.  I am running
> into some difficulties getting a primary key assigned
> for a newly created persistent object before the
> object is actually stored into the database.
> 
> Under 0.94 it seemed I could just create a new
> instance of my persistent object and then call 
> 
>       Identity id = new Identity(object);
> 
> passing the newly created object.
> 
> I did not have to do anything with the returned
> Identity (id) --- but a side effect of calling this
> was that the primaryKey of the newly created object
> was populated.
> 
> In 1.04 the above call is not available - so I changed
> it to 
> 
>         Identity id = new
> IdentityFactoryImpl(getBroker()).buildIdentity(object);
> 
> This does not seem to have the same side effect - and
> I am unsure if I can even get the primaryKey from the
> returned Identity.
> 

this was my fault. To fix a serious bug we have to change/harmonize the
behavior of Identity creation and I forget to document this change in 1.0.4.
It's no longer possible to assign the PK of an object before insert.
After insert of the object you can use the IdentityFactory to build
Identity objects.
Below I post the upcoming (OJB 1.0.5) corrected section in "sequence
manager" docs:

<snip>

To harmonize the behavior of OJB when using different SequenceManager
implementations and to solve a problem with "table per
subclass"-inheritance  we restrict the early id association in newer
versions of OJB.

since OJB 1.0.4
---------------
OJB no longer assign the PK values of transient objects on creation of
the Identity objects (using IdentityFactory). The Identity object of a
transient persistence capable object is completely independent of the
"real" primary key assigned on insert of the object - when calling

Identity oid = broker.serviceIdentity().buildIdentity(object);

the specified persistence capable object will not be modified and the
returned Identity object use transient primary key placeholder.

The reason for doing this was to "harmonize" the behavior of OJB when
using different sequence generation strategies.
E.g. when using database based identity columns (supported by MySQL,
MsSQL, HSQL, ...) it's not possible to lookup the generated PK value
before the object is written to database.

</snip>


> So I guess my question is whether or not there is a
> way to get a primaryKey for a newly created object
> prior to the object being stored into the database.
> 

Only at one's own risk. You can use:
e.g.
Identity id = new Identity(object, broker);
broker.store(ObjectModification.INSERT, object);

this should assign the PK values of 'object' before store (for all non 
database identity column based SequenceManager implementations).

but recommended usage is:
e.g.
broker.store(ObjectModification.INSERT, object);
Identity oid = broker.serviceIdentity().buildIdentity(object);

http://db.apache.org/ojb/docu/tutorials/pb-tutorial.html#Find+object+by+primary+key

regards,
Armin

> Thanks,
> 
> Gary Bartlett
> 
> 
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.