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
|