-->
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: How to get DB generated Id for persisting rows in linking ta
PostPosted: Tue Jun 12, 2007 6:11 am 
Newbie

Joined: Sun Sep 11, 2005 7:46 pm
Posts: 15
Hibernate version: 3.2.2

Name and version of the database you are using: MySQL 5.0.38

There are table A with PK column idA and table B with PK column idB. The cardinality between table A and B is ManyToMany so that I created table C with PK(idA, idB) as linking table for table A and B. There are also two additional columns in table C. All these three tables are correctly mapped as three entities, with bidirectional references between Table A/Table C and Table B/Table C. In another word, Class A has a collection of Class C and Class B has a collection of Class C too. Now there is a web form for submitting new instance of Class A. The same web form also submits the infomation which should be stored in the linking table.

idA, the PK of table A, is generated automatically by the underlying database. I am not sure how to persist this web form in an optimal way. Say, when the user filled the form, idA is still unknown, I have to persist it to get its id (do I?), all idBs would be fetched via a DB query. Then, the links with additional information will be stored into table C. My question is, how do I get the database generated idA in this unit of work? Thanks for any enlightenment!


Top
 Profile  
 
 Post subject: Re: How to get DB generated Id for persisting rows in linkin
PostPosted: Tue Jun 12, 2007 6:36 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
ellen wrote:
Hibernate version: 3.2.2

Name and version of the database you are using: MySQL 5.0.38

There are table A with PK column idA and table B with PK column idB. The cardinality between table A and B is ManyToMany so that I created table C with PK(idA, idB) as linking table for table A and B. There are also two additional columns in table C. All these three tables are correctly mapped as three entities, with bidirectional references between Table A/Table C and Table B/Table C. In another word, Class A has a collection of Class C and Class B has a collection of Class C too. Now there is a web form for submitting new instance of Class A. The same web form also submits the infomation which should be stored in the linking table.

idA, the PK of table A, is generated automatically by the underlying database. I am not sure how to persist this web form in an optimal way. Say, when the user filled the form, idA is still unknown, I have to persist it to get its id (do I?), all idBs would be fetched via a DB query. Then, the links with additional information will be stored into table C. My question is, how do I get the database generated idA in this unit of work? Thanks for any enlightenment!



After saving the object of the pojo class using session, use the method getIdentifier using session and pass the object of pojo in this. This method is used to generate DB generated Id. I had used like this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 6:53 am 
Newbie

Joined: Sun Sep 11, 2005 7:46 pm
Posts: 15
Thank you very much! I'm using JPA's EntityManager, just had a look at JPA'd javadoc, did not find any method similar to Hibernate Session's getIdentifier....But I've voted for gopal.knoah's answer anyway.

Is the JPA equivalent to Session's getIdentifier I'm looking for lying in some other class? Thanks very much for any further help!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 12:39 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
ellen wrote:
Thank you very much! I'm using JPA's EntityManager, just had a look at JPA'd javadoc, did not find any method similar to Hibernate Session's getIdentifier....But I've voted for gopal.knoah's answer anyway.

Is the JPA equivalent to Session's getIdentifier I'm looking for lying in some other class? Thanks very much for any further help!


try like this
Pojo p = new Pojo P();
session.save(P);
Integer id = (Integer)session.getIdentifier(P);


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.