-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: auto generated id and EntityManager.persist
PostPosted: Wed Mar 17, 2010 2:15 pm 
Newbie

Joined: Wed Mar 17, 2010 2:07 pm
Posts: 3
Say I persist an object with auto generated id. How do I get back the new Id?

I have
Code:
@Entity
class A {
    @Id
    @GeneratedValue
    private int id;

    ...
}


Then I do
Code:
A a = new A();
... fill in some other members of A ..
em.persist(a); // where em is an instance of entity manager


Now how do I figure out the primary key of the "a" I just saved? I need it to relate this object to another by its key. .e.g I have class B with a collection of A's. Without filling in a's id, I can't say em.persist(instanceOfB)

assume that A can only be identified uniquely by primary key.


Top
 Profile  
 
 Post subject: Re: auto generated id and EntityManager.persist
PostPosted: Wed Mar 17, 2010 2:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Just add the A's to the collection in B and then persist B. Hibernate will figure everything out automatically.


Top
 Profile  
 
 Post subject: Re: auto generated id and EntityManager.persist
PostPosted: Wed Mar 17, 2010 2:46 pm 
Newbie

Joined: Wed Mar 17, 2010 2:07 pm
Posts: 3
nordborg wrote:
Just add the A's to the collection in B and then persist B. Hibernate will figure everything out automatically.


Thanks for the quick reply. That would work just for this case. But in general (that is, without assuming everything is done in one transaction), say we have another class C that has a collection of A's as well. (I actually do). The difference is that B owns A's but C doesn't. e.g.
Code:
class B {
    @OneToMany(cascade = ALL)
    Set<A> ...;
}

class C {
    @OneToMany
    Set<A> ...; // without cascade
}



I'm doing the following:
create B, add A to B, persist
some time later
an instance of C needs to have that A I just created.

The problem is the same as before: I can't obtain a fresh version of A because both A and B are only identified uniquely by their own primary keys.


Top
 Profile  
 
 Post subject: Re: auto generated id and EntityManager.persist
PostPosted: Wed Mar 17, 2010 2:53 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not sure that I completely understand your use case... but you can force queued operations to be synchronized with the database by calling EntityManager.flush(). The id value should be available after that.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.