-->
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 retrieve the inserted record primary key value
PostPosted: Tue Feb 13, 2007 3:54 am 
Newbie

Joined: Tue Feb 13, 2007 3:44 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

<class name="com.ams.dao.general.GnlDepartment"
table="gnldepartment">
<id name="departmentid" column="department_id"
type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="departmentname" column="department_name"
type="java.lang.String" />
<property name="departmentkey" column="department_key"
type="java.lang.String" />
<property name="departmentcode" column="department_code"
type="java.lang.String" />

<property name="comments" column="comments"
type="java.lang.String" />

</class>

Code between sessionFactory.openSession() and session.close():

public static void insert(Object pobject) {
Session session = lSessionFactory.getCurrentSession();
try {
Transaction tx = session.beginTransaction();
session.save(pobject);
session.flush();
tx.commit();
} catch (HibernateException e) {
log.error("Argh ! Error occurred while creating object " + pobject
+ ": " + e);
} finally {
if (session != null && session.isOpen()) {
try {
session.close();
} catch (HibernateException e) {
log.error("Ouch ! Error occurred while closing session: "
+ e);
}
}
}
}

}

Full stack trace of any exception that occurs:

Name and version of the database you are using: Mysql 5.0

The generated SQL (show_sql=true):



Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

I am facing a problem that i need the inserted record primary key value where as i need it for displaying it.

Here the department id is the primary key value which is auto generated using hibernate.

I need that primary key value when i insert a record in a single step. please help me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 13, 2007 4:44 am 
Newbie

Joined: Tue Dec 12, 2006 3:19 am
Posts: 17
Location: Spain
Hi,
For retrieving the auto generated id I think you don't need code anything special, you may get it directly:

Code:
int id= session.save(pobject);


Or you may use the hashCode() method

Code:
int id = session.save(pobject).hashCode()

_________________
Please, rate this posting if you find it useful


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 13, 2007 6:11 am 
Newbie

Joined: Tue Feb 13, 2007 3:44 am
Posts: 2
Thank you my boy. ITs really working fine. its very useful to me.

But i have one more doubt.

How can i confirm whether the record is inserted in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 13, 2007 6:36 am 
Newbie

Joined: Tue Dec 12, 2006 3:19 am
Posts: 17
Location: Spain
As far as I know, when the commit method runs and no exception is produced, the record has to be inserted into the database.
So, having a look at your code:
Code:
   [....]
   try {
      [...]
      tx.commit();
   } catch (HibernateException e) { // <----------------- INSERT ERROR
      log.error("Argh ! Error occurred while creating object " + pobject + ": " + e);
   } finally {
   [....]


If the Exception doesn't catch any error, it means the object has to be inserted. If any exception happens, a rollback procedure (normally) is automatically run.

_________________
Please, rate this posting if you find it useful


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.