• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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???
0
JianJunShen
Asked:
JianJunShen
  • 3
1 Solution
 
JianJunShenAuthor Commented:
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.
0
 
Dejan PažinHead of SW DevelopmentCommented:

Looks like your transaction is not commited.
0
 
JianJunShenAuthor Commented:
getHibernateTemplate().flush();
But there is no commit fuction in getHibernateTemplate().
0
 
JianJunShenAuthor Commented:
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());
            
      }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now