JianJunShen
asked on
No data is inserted a Hibernate question.
I am using spring and hibernate. I have a simple table item2 and user. I could fetch the user name but I could not insert item2 data. No exception is thrown.
========================== ========== ========== ========== ========== ========== ===
public class Item2 implements Serializable {
private static final long serialVersionUID = -6542055568633475091L;
private Long iid;
private String name;
private String description;
private Integer initprice;
...}
========================== ========== ========== ========== ========== ========== ====
And its hbm.xml file is as follows:
========================== ========== ========== ========== ========== ========== =====
<?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" >
<hibernate-mapping package="com.juhani.hiberS pring2.hib ernate">
<class name="Item2" table="item2">
<id name="iid" column="iid" type="long">
<generator class="native" />
</id>
<property name="name" column="name" type="string" />
<property name="description" column="description" type="string" />
<property name="initprice" column="initprice" type="integer" />
</class>
</hibernate-mapping>
========================== ========== ========== ========== ========== =========
And I write a test application as follows:
========================== ========== ========== ========== ========== ====
public void saveItem2() throws Exception {
TransactionSynchronization Manager.
bindResource(getSessionFac tory(), new SessionHolder(getSession() ));
Item2 item = new Item2();
item.setName("tShirt");
item.setDescription("a good one");
item.setInitprice(20);
getHibernateTemplate().sav e(item);
getHibernateTemplate().flu sh();
TransactionSynchronization Manager.un bindResour ce(getSess ionFactory ());
}
========================== ========== ========== ========== ========== ========== =========
But there is no data is inserted. Why???
==========================
public class Item2 implements Serializable {
private static final long serialVersionUID = -6542055568633475091L;
private Long iid;
private String name;
private String description;
private Integer initprice;
...}
==========================
And its hbm.xml file is as follows:
==========================
<?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" >
<hibernate-mapping package="com.juhani.hiberS
<class name="Item2" table="item2">
<id name="iid" column="iid" type="long">
<generator class="native" />
</id>
<property name="name" column="name" type="string" />
<property name="description" column="description" type="string" />
<property name="initprice" column="initprice" type="integer" />
</class>
</hibernate-mapping>
==========================
And I write a test application as follows:
==========================
public void saveItem2() throws Exception {
TransactionSynchronization
bindResource(getSessionFac
Item2 item = new Item2();
item.setName("tShirt");
item.setDescription("a good one");
item.setInitprice(20);
getHibernateTemplate().sav
getHibernateTemplate().flu
TransactionSynchronization
}
==========================
But there is no data is inserted. Why???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
getHibernateTemplate().flu sh();
But there is no commit fuction in getHibernateTemplate().
But there is no commit fuction in getHibernateTemplate().
ASKER
Yes, you are absolutely right. It is transaction missing. Detail codes is following:
public void saveItem2() throws Exception {
TransactionSynchronization Manager.
bindResource(getSessionFac tory(),
new SessionHolder(getSession() ));
getSession().beginTransact ion();
Item2 item = new Item2();
item.setName("tShirt");
item.setDescription("a good one");
item.setInitprice(20);
getHibernateTemplate().sav e(item);
getHibernateTemplate().flu sh();
getSession().getTransactio n().commit ();
TransactionSynchronization Manager.un bindResour ce(getSess ionFactory ());
}
public void saveItem2() throws Exception {
TransactionSynchronization
bindResource(getSessionFac
new SessionHolder(getSession()
getSession().beginTransact
Item2 item = new Item2();
item.setName("tShirt");
item.setDescription("a good one");
item.setInitprice(20);
getHibernateTemplate().sav
getHibernateTemplate().flu
getSession().getTransactio
TransactionSynchronization
}
ASKER
Hibernate: select user0_.uid as uid3_0_, user0_.name as name3_0_, user0_.password as password3_0_ from user user0_ where user0_.uid=?
juhani@yahoo.com
Hibernate: insert into item2 (name, description, initprice) values (?, ?, ?)
As one could see, there is no problme for fetch user table. But it could not insert data though SQL looks fine.