-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to get primary key of newly inserted object
PostPosted: Thu May 10, 2007 10:52 pm 
Newbie

Joined: Thu May 10, 2007 10:46 pm
Posts: 4
In Hibernate EntityManager, I use "entityManager.persist(obj)" to insert a newly created obj to database. If the primary key is auto generated by database, is there any way to retrieve the key?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 4:37 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
You should be able to just save/persist the object and then call getId()...

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 5:07 am 
Newbie

Joined: Thu May 10, 2007 10:46 pm
Posts: 4
the "save" method can not be found in EntityManager class, should other class be used here?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 5:35 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Use some DAOs, personaly I have mine extend Spring's HibernateDaoSupport and then my save method would look like:

Code:
    public void save(CostPeriod transientInstance) {
        if (log.isDebugEnabled()) {
          log.debug("saving CostPeriod instance");
        }
       
        getHibernateTemplate().saveOrUpdate(transientInstance);
        if (log.isDebugEnabled()) {       
           log.debug("save successful");
        }
    }

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 10:20 am 
Newbie

Joined: Tue May 23, 2006 3:41 am
Posts: 6
Adam,
could you explain more detail, with example desirable, how it works.
I mean my DAO implementation based on Entity manager and as I understand correctly, if I will use your DAO support - I should refuse Entity manager or I somethink missed


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 12:11 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
Dude, relax. You should be just fine with your EntityManager. It has different naming but the same meaning. Once you invoke entityManager.persist(someInstance), you should be able to get someInstance's id by calling someInstance.getId() -- assuming your ID property is named id. If for some reason this does not work, flush() the entity manager before calling getId(). Just take it easy ;)

Henrique

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 11:55 am 
Newbie

Joined: Thu May 10, 2007 10:46 pm
Posts: 4
hlfsousa wrote:
Dude, relax. You should be just fine with your EntityManager. It has different naming but the same meaning. Once you invoke entityManager.persist(someInstance), you should be able to get someInstance's id by calling someInstance.getId() -- assuming your ID property is named id. If for some reason this does not work, flush() the entity manager before calling getId(). Just take it easy ;)

Henrique

Here is my code:
Code:
entityManager.persist(transientInstance);
entityManager.flush();
log.debug("persist successful:" + transientInstance.getId());


The generated id in DB is 10, transientInstance.getId() gives 0. Did I mistake anything here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 3:02 am 
Newbie

Joined: Tue May 23, 2006 3:41 am
Posts: 6
Ye, Henrique was write- we should relax :O)
Just forget transientInstance.
Code:
        Epistle epi = new Epistle();
        epi.setEpiContent(proba);
        epi.setEpiFooter(probaFooter);
        epi.setEpiHeader(probaHeader);
        em.persist(epi);
       log.debug("persist successful:" + epi.getId());

Here Epistle - is table from DB, em- EntityManager


Top
 Profile  
 
 Post subject: It worked
PostPosted: Mon May 14, 2007 10:53 am 
Newbie

Joined: Thu May 10, 2007 10:46 pm
Posts: 4
I found the problem - I didn't specify ID generation type in Object class definition.
Code:
@Id
   @Column(name = "id", unique = true, nullable = false)
   public int getId() {
      return this.id;
   }


It worked after putting ID generation Type:
Code:
   @Id [b]@GeneratedValue(strategy=GenerationType.AUTO)[/b]
   @Column(name = "id", unique = true, nullable = false)
   public int getId() {
      return this.id;
   }


Thanks for all the help!


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