Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » retrieving the assigned primary key
retrieving the assigned primary key [message #489625] Mon, 05 October 2009 11:58 Go to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
I've moved a piece of code to native queries because of locking and conflicts with Eclipselinks' cache. But on the boundary between Eclipselink and native I need to be able to access the primary keys that Eclipselink assigned to newly inserted entities. In this specific scenario it's the PK of an entity that is persisted by reference in a 1-N relation. Is there any way to find the PK that is assigned to a entity immediately after the pk was created from the sequence table?

The other approach would be for me to set that primary key myself. That I will test now.

Tom
Re: retrieving the assigned primary key [message #489649 is a reply to message #489625] Mon, 05 October 2009 13:35 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You can access the assigned Id from your object after calling persist() (or flush() if using IDENTITY).

You could also use the PostPersist event.


James : Wiki : Book : Blog : Twitter
Re: retrieving the assigned primary key [message #489692 is a reply to message #489649] Mon, 05 October 2009 14:59 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
James wrote:
> You can access the assigned Id from your object after calling persist()
> (or flush() if using IDENTITY).
>
> You could also use the PostPersist event.

I assumed that too, but I never call persist myself. Setting it myself works fine, but I wonder where the automatically assigned PK went.
Re: retrieving the assigned primary key [message #491695 is a reply to message #489692] Thu, 15 October 2009 13:50 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Hi all,

I have a question similar to this. I

I wouuld retrive the assigned primary key for an entity that I have
persisted but it has been persisted because insede a relationship.

I mean, I have two entity A and B. A has a list of B entities.
I add a B entity in the A list entity then I persist the A Entity
(cascade all). So, the new persisted entity is an B entity. I would
retrieve immediately the primary key of the B entity, but it seems that
after persist and also flush the B entity object has not acquired the
rimary key.
Obviously I can try using a findbyexample after persisting the A entity,
but I prefer to have the primary key on the B object after persist.

Best Regards,
Enrico

tbee ha scritto:
> James wrote:
>> You can access the assigned Id from your object after calling
>> persist() (or flush() if using IDENTITY).
>>
>> You could also use the PostPersist event.
>
> I assumed that too, but I never call persist myself. Setting it myself
> works fine, but I wonder where the automatically assigned PK went.
>
Re: retrieving the assigned primary key [message #491696 is a reply to message #491695] Thu, 15 October 2009 13:53 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
For me the set-primary-key-myself worked fine, but indeed indirectly persisted entities did not receive a PK.

Tom
Re: retrieving the assigned primary key [message #492215 is a reply to message #491696] Mon, 19 October 2009 13:48 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Objects persisted through cascade persist will still have an Id assigned. If using IDENTITY you must call flush().

If you are not noticing the Id assigned, then what is being inserted into the database on flush or commit?

Are you should your relationship is cascade persist, also check to ensure you don't have multiple copies of the same object registered.



James : Wiki : Book : Blog : Twitter
Previous Topic:EMF/Teneo and EclipseLink integration
Next Topic:Random deadlock/hang during commit in EJB3 application
Goto Forum:
  


Current Time: Tue Mar 19 05:19:06 GMT 2024

Powered by FUDForum. Page generated in 0.02529 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top