Link to home
Start Free TrialLog in
Avatar of JianJunShen
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.hiberSpring2.hibernate">
    <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 {
            TransactionSynchronizationManager.
            bindResource(getSessionFactory(), new SessionHolder(getSession()));
            Item2 item = new Item2();
            item.setName("tShirt");
            item.setDescription("a good one");
            item.setInitprice(20);
            getHibernateTemplate().save(item);      
            getHibernateTemplate().flush();
            TransactionSynchronizationManager.unbindResource(getSessionFactory());
            
      }
=====================================================================================
But there is no data is inserted.  Why???
Avatar of JianJunShen
JianJunShen

ASKER

And output window shows following message:

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.
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
getHibernateTemplate().flush();
But there is no commit fuction in getHibernateTemplate().
Yes, you are absolutely right. It is transaction missing. Detail codes is following:

      public void saveItem2() throws Exception {
            TransactionSynchronizationManager.
            bindResource(getSessionFactory(),
                               new SessionHolder(getSession()));
            getSession().beginTransaction();
            Item2 item = new Item2();
            item.setName("tShirt");
            item.setDescription("a good one");
            item.setInitprice(20);
            getHibernateTemplate().save(item);      
            getHibernateTemplate().flush();
            getSession().getTransaction().commit();
            TransactionSynchronizationManager.unbindResource(getSessionFactory());
            
      }