日期:2014-05-17  浏览次数:20723 次

Hibernate 保存不出错,但数据库没有数据是什么原因
Dept d =new Dept();
d.setDeptId("999999");
d.setDeptCode ("999999");
d.setDeptName ("999999");
d.setHospitalId("1");
DeptDAO a =new DeptDAO();

Transaction tx = a.getSession().beginTransaction();
try {
a.save(d);
} catch (Exception e) {
tx.rollback(); 

tx.commit();

2012-02-05 15:43:40,390 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - Not binding factory to JNDI, no JNDI name configured
2012-02-05 15:43:40,390 DEBUG [org.hibernate.impl.SessionFactoryImpl] - instantiated session factory
2012-02-05 15:43:40,390 DEBUG [org.hibernate.impl.SessionFactoryImpl] - Checking 0 named HQL queries
2012-02-05 15:43:40,390 DEBUG [org.hibernate.impl.SessionFactoryImpl] - Checking 0 named SQL queries
2012-02-05 15:43:40,390 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 13284278203
2012-02-05 15:43:40,390 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2012-02-05 15:43:40,390 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - generated identifier: 999999, using strategy: org.hibernate.id.Assigned
2012-02-05 15:43:40,406 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [Map.Dept#999999]
2012-02-05 15:43:40,421 DEBUG [Map.DeptDAO] - save successful
2012-02-05 15:43:40,421 DEBUG [org.hibernate.transaction.JDBCTransaction] - commit
2012-02-05 15:43:40,421 DEBUG [org.hibernate.impl.SessionImpl] - automatically flushing session
2012-02-05 15:43:40,421 DEBUG [org.hibernate.jdbc.JDBCContext] - before transaction completion
2012-02-05 15:43:40,421 DEBUG [org.hibernate.impl.SessionImpl] - before transaction completion
2012-02-05 15:43:40,421 DEBUG [org.hibernate.transaction.JDBCTransaction] - committed JDBC Connection
2012-02-05 15:43:40,421 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction completion
2012-02-05 15:43:40,421 DEBUG [org.hibernate.jdbc.ConnectionManager] - aggressively releasing JDBC connection
2012-02-05 15:43:40,421 DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2012-02-05 15:43:40,421 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - returning connection to pool, pool size: 1
2012-02-05 15:43:40,421 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion

------解决方案--------------------
你写的是神马乱七八糟的代码啊?????
好好看看Hibernate自带的demo里面是怎么写的
org.hibernate.auction.Main这个类:
Java code

    /**
     * Demonstrates detached object support
     */
    public void changeUserDetails(User user) throws Exception {
        System.out.println("Changing user details for: " + user.getId() );

        Session s = factory.openSession();
        Transaction tx=null;
        try {
            tx = s.beginTransaction();

            s.merge(user);

            tx.commit();
        }
        catch (Exception e) {
            if (tx!=null) tx.rollback();
            throw e;
        }
        finally {
            s.close();
        }
    }

------解决方案--------------------
把DeptDAO 贴一下看看
------解决方案--------------------
检查事务有没提交。


hibernate有一个自动提交事务的配置

<property name="hibernate.connection.autocommit">true</property>
------解决方案--------------------
你们没发现他些的代码顺序都有问题???事务都没提交进行回滚呢?还有Hibernate的所有操作都离不开session,他的session呢?只是在开启事务那里用到了,最后还没有关闭session 。。。。
------解决方案--------------------
Not binding factory to JNDI, no JNDI name configured