-->
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.  [ 2 posts ] 
Author Message
 Post subject: Adding data in two records of same table
PostPosted: Fri Sep 23, 2005 4:15 am 
Newbie

Joined: Fri Sep 23, 2005 3:33 am
Posts: 1
Location: Pakistan
Hi

I am new to Hibernate, I am using Struts, Hibernate, and MySQL for a project.

I have a form with different fields. After submitting I want to break the data and add in two records of the same table. I appreciate if someone help me.

Thanks

Anosh

Following are the information for you.

Hibernate version:
3.0.5

Mapping documents:
<class name="Data" table="data">
<id name="dataid" type="java.lang.Integer" column="dataid">
<generator class="increment"/>
</id>

<property name="datadate" column="datadate" type="java.util.Date" />
<property name="voucherid" column="voucherid" type="java.lang.Integer" />
<property name="headcodedr" column="headcodedr" type="java.lang.Integer" />
<property name="headcodecr" column="headcodecr" type="java.lang.Integer" />
<property name="amount" column="amount" type="java.lang.Integer" />
<property name="description" column="description" type="java.lang.String" />
</class>


Code between sessionFactory.openSession() and session.close():
public void saveData(Data data) {
Session session = null;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
if (data.getDataid() == null || data.getDataid().intValue() == 0)
session.save(data);
else {
Data toBeUpdated = (Data) session.get(Data.class, data.getDataid());
toBeUpdated.setDatadate(data.getDatadate());
toBeUpdated.setVoucherid(data.getVoucherid());
toBeUpdated.setHeadcodedr(data.getHeadcodedr());
toBeUpdated.setHeadcodecr(data.getHeadcodecr());
toBeUpdated.setAmount(data.getAmount());
toBeUpdated.setDescription(data.getDescription());
session.update(toBeUpdated);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null) try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}

} finally {
try {
if (session != null) session.close();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
}


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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:48 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
What is the behaviour of your code and what behaviour do you want?

_________________
Lars Fiedler


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