-->
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.  [ 6 posts ] 
Author Message
 Post subject: how to get the auto increment key value
PostPosted: Tue Jun 12, 2007 9:10 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
Hibernate version: 3.1

Mapping documents:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.DAO.account.Contact" table="contact" catalog="pao">

<id name="contactid" type="java.lang.Integer">
<column name="CONTACTID" />
<generator class="native" />
</id>
<property name="admoid" type="java.lang.Integer">
<column name="ADMOID" />
</property>
<property name="characteristics" type="java.lang.String">
<column name="CHARACTERISTICS" length="180" />
</property>
<property name="commid" type="java.lang.String">
<column name="COMMID" length="3" />
</property>
<property name="communicationtype" type="java.lang.Short">
<column name="COMMUNICATIONTYPE" />
</property>
<property name="contacttype" type="java.lang.String">
<column name="CONTACTTYPE" length="20" />
</property>
<property name="deleted" type="java.lang.Short">
<column name="DELETED" />
</property>
<property name="dispname" type="java.lang.String">
<column name="DISPNAME" length="40" />
</property>
<property name="firstname" type="java.lang.String">
<column name="FIRSTNAME" length="15" />
</property>
<property name="formalinformalcomm" type="java.lang.String">
<column name="FORMALINFORMALCOMM" length="30" />
</property>
<property name="langcode" type="java.lang.String">
<column name="LANGCODE" length="2" not-null="true" />
</property>
<property name="mutdat" type="java.util.Date">
<column name="MUTDAT" length="10" />
</property>
<many-to-one name="personkindid" column="PERSONKINDID" class="com.DAO.account.Kindofperson">
</many-to-one>
<property name="priorityid" type="java.lang.Short">
<column name="PRIORITYID" />
</property>
<property name="searchcode" type="java.lang.String">
<column name="SEARCHCODE" length="10" not-null="true" />
</property>
<property name="sexcode" type="java.lang.Short">
<column name="SEXCODE" />
</property>
<property name="surname" type="java.lang.String">
<column name="SURNAME" length="40" not-null="true" />
</property>
<many-to-one name="titleid" column="TITLEID" class="com.DAO.account.Titles">
</many-to-one>
</class>
</hibernate-mapping>




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

public static void insert(Object pobject,HttpServletRequest request) {
Session session;
ServletContext context=request.getSession().getServletContext();
SessionFactory _factory = (SessionFactory) context.getAttribute("SESSION_FACTORY_KEY_CRM");
session=_factory.openSession();
try {
Transaction tx = session.beginTransaction();
System.out.println("*****************insert method of hibernate *********************");
session.save(pobject);
session.flush();
tx.commit();
System.out.println("*****************insert completed *********************");
} catch (HibernateException e) {
System.out.println("Argh ! Error occurred while creating object " + pobject + ": " );
e.printStackTrace();
} finally {
if (session != null && session.isOpen()) {
try {
session.close();
} catch (HibernateException e) {
System.out.println("Ouch ! Error occurred while closing session: ");
e.printStackTrace();
}
}
}
}



Name and version of the database you are using: mysql 5


The values are inserted in the contact table.

My problem is i need the auto incremented contactid value after a record is inserted. Because i need to use the id to insert in the child table.
pls help me.


Top
 Profile  
 
 Post subject: The generated identifier
PostPosted: Tue Jun 12, 2007 9:26 am 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:50 am
Posts: 30
Location: Minneapolis, MN
Hi,
The save method returns the generated identifier after the insert.
The signature of the save method is

public Serializable save(Object object) throws HibernateException

Parameters:
object - a transient instance of a persistent class
Returns:
the generated identifier

Please read the API for more details

**Don't forget to rate if you find the solution useful **
Regards,
Paras


Top
 Profile  
 
 Post subject: Re: The generated identifier
PostPosted: Tue Jun 12, 2007 9:50 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
Where can i get the generated identifier will u pls write me a sample to get the identifier and to print the id value


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 10:13 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Hi,

strange people... :-)

Transaction tx = session.beginTransaction();
System.out.println("*****************insert method of hibernate *********************");
Integer id = (Integer)session.save(pobject);
session.flush();
tx.commit();
System.out.println("*****************insert completed. Object created with id +" id ".+ *********************");


Actually this is almost the same what parasjain01 has posted...

Regards,

Giorgi


Top
 Profile  
 
 Post subject: Identifier value
PostPosted: Tue Jun 12, 2007 10:57 am 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:50 am
Posts: 30
Location: Minneapolis, MN
Sorry, I forgot to write the sample code and explanation.
Actually after insertion hibernate will return the Id value. You were not collection that id value in any variable. By doing
Integer id = (Integer)session.save(pobject);
you can collect that value in variable id

Thanks Javri for the code.

Regards,
Paras


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 2:27 pm 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
THANK YOU VERY MUCH


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