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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate is not returning GeneratedValue
PostPosted: Thu Jan 10, 2008 5:07 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I have a problem with @GeneratedValue id field, because when it is saved in the database, the id value in the object which is saved is still null (even if it is generated in database correctly) ? How can I get that generated Id value ?

I have a simple class:


Code:
@javax.persistance.Entity
public class Person implements java.io.Serializable {

private static final long serialVersionUID = 42314234234123422342134L;
   
   private Long id;

   private String name;

   public Test() {

   }


   @javax.persistence.Id
   @javax.persistence.GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() {
      return this.id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getName() {
      return this.name;
   }

   public boolean equals(Object o) { ... }
   
   public int hashCode() { ...}

}


and my test class:


Code:
public class TestCase {

private EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("punit");
private EntityManager em =  emFactory.createEntityManager();

   
   @Test
   public void testSave() {
      em.getTransaction().begin();

      Person p = new Person();
      p.setName("John");

      em.persist(rec1);

      em.getTransaction().commit();

      assertNotNull(p.getId()); <- THIS FAILS!!!

      em.close();
      emFactory.close();
   }
}



My configuration:

Eclipse Europa 3.3 + jdk 1.5.0_12
JUnit 4
Hibernate 3.2.5 GA
Hibernate Annotations 3.3.0 GA
Hibernate EntityManager 3.3.1 GA
MySQL 5.0.45 + mysql-connector-java-5.0.7
WinXP SP2


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 12:00 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
Nobody knows solution for this problem ?!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 14, 2008 12:45 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
As far as I can see, if I have a newly created Person object, and when I call merge of this object, it writes it, but it doesn't return id value into the object from database. If I call persist, then it returns, why is that ?

Is it possible to call merge and that generated value be returned ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 1:43 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What database are you using? Your example should work.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 2:20 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I said up that I'm using: MySQL 5.0.45 + mysql-connector-java-5.0.7.

I checked it 100 times! When I use merge on new object GeneratedValue is not returned to object from database when it is saved, and when I used persit it works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 2:22 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
MilanMilanovich wrote:
I said up that I'm using: MySQL 5.0.45 + mysql-connector-java-5.0.7.

I checked it 100 times! When I use merge on new object GeneratedValue is not returned to object from database when it is saved, and when I used persit it works.


When you merge you have to use the returned object, the passed object still stays in a detached state. Have you tested the returned object?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 8:52 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
Dear Farzad,

I tested it like this:

Code:
Person p = new Person();
p.setName("John");

em.merge(rec1);

em.getTransaction().commit();

assertNotNull(p.getId()); <- THIS FAILS!!!


As you can see above, Id fails.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 11:38 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
How about this:

Code:
p = em.merge(p);





Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 4:11 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
farzad wrote:
How about this:

Code:
p = em.merge(p);


Farzad-


Well, if I do this:

Code:
Person p = new Person();
p.setName("John");

p = em.merge(rp);

em.getTransaction().commit();

assertNotNull(p.getId()); <- THIS FAILS!!!


It still fails to return autogenerated ID!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 4:37 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What is the rp object you pass in merge?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 7:27 pm 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
farzad wrote:
What is the rp object you pass in merge?


Farzad-



Sorry, type mistake, it is p. Actual code is this:

Code:
Person p = new Person();
p.setName("John");

p = em.merge(p);

em.getTransaction().commit();

assertNotNull(p.getId()); <- THIS FAILS!!!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 27, 2008 9:12 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am a little confused here because I do quite a similar test and it passes. Can you use a jdbc logger like p6spy and tell me what jdbc communications you have with MySQL database?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 7:58 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
Dear Farzad,

yes, you are right, it was my mistake. Insted of

Code:
p = em.merge(p);


I just called:

Code:
em.merge(p);


But, now another problem emerges, when I save object in this way in my JUnit test methods, and want to use some p methods, like:

Code:
Iterator<Class> iter = p.getList().iterator(); (some simple list with OneToMany (cascade=all) option)

Class c = iter.next(); <- HERE I GOT THE EXCEPTION BELOW!!!


I (sometimes) got an exception:

Code:
java.util.ConcurrentModificationException
       at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
       at java.util.HashMap$KeyIterator.next(Unknown Source)
       at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistenctCollection.java:555)
at com.test.PersonTest

....

Why I now get this error ? Is it possible that after caling this "merge" method in the way above, I got actually two instances of p ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 11:50 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
MilanMilanovich wrote:
Why I now get this error ? Is it possible that after caling this "merge" method in the way above, I got actually two instances of p ?




I can't tell much from this. Can you show me the codes and the updated code for the entity classes?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 11:55 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
Hi Farzad,

as I have debuged it, the problem is next: object
Code:
p
of type Person have a list (collection wich cascades to some other type), and when I call merge like this:
Code:
p = em.merge(p);
two instances of
Code:
p
object are created, but it seems that collection is the same. And then when I want to change the collection in
Code:
p
, like:
Code:
p.getCollection().remove...
in this case it reports that ConcurrectException.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.