?
Solved

No data is inserted a Hibernate question.

Posted on 2007-07-27
4
Medium Priority
?
272 Views
Last Modified: 2010-03-30
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
Comment
Question by:JianJunShen
  • 3
4 Comments
 

Author Comment

by:JianJunShen
ID: 19580681
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
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 total points
ID: 19580753

Looks like your transaction is not commited.
0
 

Author Comment

by:JianJunShen
ID: 19580821
getHibernateTemplate().flush();
But there is no commit fuction in getHibernateTemplate().
0
 

Author Comment

by:JianJunShen
ID: 19580864
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question