-->
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.  [ 7 posts ] 
Author Message
 Post subject: ID not set after a save
PostPosted: Fri Jun 10, 2005 2:29 pm 
Newbie

Joined: Tue Jun 07, 2005 1:08 pm
Posts: 10
Hello,

I'm trying a very simple insert of an object and I'm noticing that the ID of the object is not being set in my pojo after the call to save (actually a call to Session.save or Session.persist, although Session.save does return the serializable ID). I thought that hibernate was supposed to set the ID after the object is persisted? Am I wrong in my understanding?

Here's what my object looks like:

Code:
public class TestObject {
    protected long id;
    protected String name;
   
    public TestObject() {       
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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


And my mapping...

Code:
   <class name="com.tickets.audit.TestObject" table="TestObject">
      <id type="long" column="id">
         <generator class="sequence"/>
      </id>
      <property name="name"
         column="name"
         type="string"/>
   </class>


Yet when I run my test and make a call to persist or save, the object is saved correctly in the database, but the id field is not set. Can someone please shed some light on this?

Thank you,

John


Top
 Profile  
 
 Post subject: Re: ID not set after a save
PostPosted: Fri Jun 10, 2005 3:51 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
jmikhail wrote:
Hello,

I'm trying a very simple insert of an object and I'm noticing that the ID of the object is not being set in my pojo after the call to save (actually a call to Session.save or Session.persist, although Session.save does return the serializable ID). I thought that hibernate was supposed to set the ID after the object is persisted? Am I wrong in my understanding?

...
...

Yet when I run my test and make a call to persist or save, the object is saved correctly in the database, but the id field is not set. Can someone please shed some light on this?

Thank you,

John


Can you show the code you use to persist the object ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 4:52 pm 
Newbie

Joined: Tue Jun 07, 2005 1:08 pm
Posts: 10
Code snippet below:

Code:
    public void testTestObject() {

       
        TestObject ao = null;
       
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            ao = new TestObject();
            ao.setName("John Doe");
            // Also tried session.persist and session.saveOrUpdate...same behavior
            session.save(ao);
            tx.commit();                   
       
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
            Assert.fail(ex.getMessage());
        }       
             
        Assert.assertNotNull("TestObject was not created", ao);
        Assert.assertTrue("TestObject ID not set", ao.getId().length() > 0);       
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 4:54 pm 
Newbie

Joined: Tue Jun 07, 2005 1:08 pm
Posts: 10
BTW, just to clarify...I'm not having a problem persisting the data in the database. The record gets created correctly with the correct ID. My problem is my pojo doesn't have the ID set after the call to save.

this happens with all sorts of different generators (uuid.hex, increment, sequence, etc)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 5:32 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
jmikhail wrote:
BTW, just to clarify...I'm not having a problem persisting the data in the database. The record gets created correctly with the correct ID. My problem is my pojo doesn't have the ID set after the call to save.

this happens with all sorts of different generators (uuid.hex, increment, sequence, etc)


I'm a little confused here. Your class has the getId() method returning a primitive long value and your TestCase has you executing a ao.getId().length() method on it ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 5:42 pm 
Newbie

Joined: Tue Jun 07, 2005 1:08 pm
Posts: 10
pksiv wrote:

I'm a little confused here. Your class has the getId() method returning a primitive long value and your TestCase has you executing a ao.getId().length() method on it ?


Sorry, I was in the middle of making some changes when I cut and pasted the code. I had changed my id from a sequence to a uuid, so I had it defined as a string.

The assert should just be checking for ao.getId() > 0. But it's the same when I had the id defined as a String for use with a uuid.hex generator.

John


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 8:32 pm 
Newbie

Joined: Tue Jun 07, 2005 1:08 pm
Posts: 10
I figured out the problem. Here's the solution in case anyone else runs into this problem.

Turns out my mapping file was somewhat incorrect. Actually it wasn't incorrect, it was missing something. Before it looked like:

Code:
   <class name="com.tickets.audit.TestObject" table="TestObject">
      <id type="long" column="id">
         <generator class="sequence"/>
      </id>
      <property name="name"
         column="name"
         type="string"/>
   </class>


Turns out instead of specifying column="id", I should have said name="id". I assumed that if have column in there, that Hibernate would know what attribute to set in the pojo. I figured since it works that way if I specified only name, that it would work the other way around. So now my mapping looks like:

Code:
   <class name="com.tickets.audit.TestObject" table="TestObject">
      <id type="long" name="id" column="id">
         <generator class="sequence"/>
      </id>
      <property name="name"
         column="name"
         type="string"/>
   </class>


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