-->
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.  [ 8 posts ] 
Author Message
 Post subject: ID still null after persistent
PostPosted: Thu Feb 15, 2007 7:59 pm 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Why is my object not update with given id after saving is to db? I have checked the table, and the new row is there but my object's id is still null.

Im using Mysql 5 and Annotations on id.
Code:
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)


Code:
Car car = new Car("Car Name");
em.persist(car);//table updated
System.out.println(car.getId()); // null


Thanks for your reply!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 8:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
flush in between
or use session.save for such a semantic

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 4:48 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Im getting the id from db, but only on the applicationserver, i dosent go back to the original object on the webserver. This must be an common problem in the EJB-world? Is there any sollutions for this? Or must i return the object i just saved?

Thanks for your reply!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 6:36 pm 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Guess I'll have to figure this one out by my self. Thanks anayway.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 2:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't understand your question :-)
I *guess* the answer is that you need to flush before serializing your object.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: JPA does not update the original instance!
PostPosted: Sat Mar 31, 2007 7:57 pm 
Newbie

Joined: Sat Mar 24, 2007 1:10 pm
Posts: 5
in opposite to hibernate, JPA does not update the original object instance after persisting. therefore you have to do

Car car = new Car("Car Name");
car = em.persist(car); // <-- notice the car =
out.println(car.getId());

regards
stefan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 01, 2007 9:49 pm 
Beginner
Beginner

Joined: Fri Jan 27, 2006 4:20 am
Posts: 22
emmanuel is right, you have to call the flush method after calling persist method.

Since you have GenerationType.AUTO, JPA needs to select the newly assigned ID (by mysql increment) by calling entityManager.flush() after calling persist. JPA will not know the newly assigned ID after you call the flush method since you delegated the ID assignment to MySQL.


Top
 Profile  
 
 Post subject: Re: ID still null after persistent
PostPosted: Wed Jul 16, 2014 3:22 pm 
Newbie

Joined: Wed Jul 16, 2014 3:17 pm
Posts: 1
Stephan's suggestion worked for me. The persist or save method of my DAO returns the entity that I saved with the new Id.

Thanks!!

-------------------------------------------


in opposite to hibernate, JPA does not update the original object instance after persisting. therefore you have to do

Car car = new Car("Car Name");
car = em.persist(car); // <-- notice the car = the returned value from the persist method.....
out.println(car.getId());

regards
stefan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.