-->
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: Retrieve Value of Inserted Record
PostPosted: Thu Jun 05, 2008 4:07 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Hi,
I am new to hibenate. so, i need some help. I am storing value in table using Annotations and EntityManagerAPI.

I am storing the name, age in user table, in that table id is primary key and also autoincreament value. so, i am doing that using below way :

EntityManager entityManager = null;
User user = new User();
user.setName("abc");
user.setAge(12);
---
some code
---
entityManager.persist(user)

In above code, id is primay key and i am not setting id in user object. so , how i will retrieve the value of that primary key, which is inserted in above record ? Do i need to again submit second query using find method ?

Is there any way, that give us value primay key of inserted record without submitting second query ?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 5:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

just call getId() on the persisted object. For a more detailed discussion on the subject see http://forum.hibernate.org/viewtopic.php?t=986388.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 7:14 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Thanks for reply.

I tried below different way, but still not working.

1
entityManager.persist(user);
user.getId();
entityManager.commit();

2
Session session = (Session)entityManager.getDelegate();
int id = ((Integer)session.getIdentifier(user)).intValue();

3
User user1 = (User)entityManager.merge(user);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 7:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

maybe you could provide some more information, for example the annotated classes, which db you are using and which version of hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 7:54 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Hi,

i am using hibernate - 3.2 and hibernateEntityManager-3.3.2.CR1
and MySQL Database.

Bena class :
@Entity
@Table(name = "wiklet", catalog = "my_db")
public class Wiklet implements java.io.Serializable {

private long wikletId;

private ContactDetails contact;
private Project project;
private Integer dekiPageId;

public Wiklet() {
}

public Wiklet(long wikletId) {
this.wikletId = wikletId;
}

@Id
@Column(name = "wiklet_id", unique = true, nullable = false, precision = 10, scale = 0)
public long getWikletId() {
return this.wikletId;
}

public void setWikletId(long wikletId) {
this.wikletId = wikletId;
}
}

and my code :

public void store(Object object){

EntityManager entityManager = null;

entityManager = JPAEntityManagerFactory.getEntityManager();

EntityTransaction transaction = entityManager.getTransaction();

transaction.begin();
entityManager.persist(object);
transaction.commit();

}

and in call to above method, i am passing bean class object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 8:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

do you use any other mapping files, or where do you define the id generator strategy. If you just want to use annotations try adding @GeneratedValue after @Id. See also http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-identifier.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 8:52 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Hi,

i just change in Id annothations .

@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "wiklet_id", unique = true, nullable = false, precision = 10, scale = 0)
public long getWikletId() {
return this.wikletId;
}

and it works :-)

Thanks for help, but still i didnt understant the problem, so can u pls tell me that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 8:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Without @GeneratedValue Hibernate assumes you generate the ids manually.


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.