【问题标题】:hibernate session.save() does not reflect in databasehibernate session.save() 不反映在数据库中
【发布时间】:2013-04-22 23:00:41
【问题描述】:

根据我在hibernate中的理解(请确认)

1- 如果您通过getSessionFactory().openSession() 获得,则必须session.close()
2- 如果您通过getSessionFactory().getCurrentSession() 获得它,则无需session.close()commit() 后自动关闭。

3- @2 当使用getSessionFactory().getCurrentSession() 时,我们必须在活动事务中执行所有数据库活动,以便我们可以在最后提交()。

4- Hibernate 将所有保存、更新和删除操作排入队列,并仅在 flush() 操作或 提交事务或关闭会话发生这些操作。(根据javadoc)

从以上几点,如果我考虑 1 & 4,那么下面的代码应该可以工作:

Session session = HibernateUtil.getSessionFactory().openSession();  
AccountDetails ac = new AccountDetails();  
//perform set operations  
session.save(ac);  
session.close();  
System.out.println("new account stored.");

但是它不工作,即它运行平稳但没有反映(存储)在数据库中。为什么会这样?当我在事务中编写代码并提交时,它就会被存储。

我想我错过了一个基本的东西。请澄清。

【问题讨论】:

标签: java hibernate session


【解决方案1】:

当您使用 SessionFactory.openSession() 创建会话时,不会创建任何事务,因此您的操作是在事务上下文之外执行的。为了查看您的更改,您必须启动一个新事务,或者将您的操作作为正在进行的事务的一部分执行。来自文档:

典型的交易应该使用以下成语:

Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

【讨论】:

    【解决方案2】:

    我也试图理解这一点:save() 可以在事务边界之外执行插入操作所以我做了这样的事情

    SessionFactory factory = new Configuration().configure()
                        .buildSessionFactory();
                Session session = factory.openSession();
    
                session.save(user);
    
                session.close();
    

    但是数据没有插入数据库。所以我再次尝试了这个,现在它对我有用并且数据插入成功:

    在配置文件中:

      <property name="connection.autocommit">true</property>
    

    在java代码中:

        SessionFactory factory = new Configuration().configure()
                .buildSessionFactory();
        Session session = factory.openSession();
    
        session.save(user);
    
        session.flush();
        session.close();
    

    【讨论】:

      【解决方案3】:

      是的 session.save 在没有事务的情况下不会保存到数据库中。 唯一的方法是使用

      <property name="connection.autocommit">true</property>
      

      如果使用此属性,则无需交易,也不需要session.flush()session.close()

      【讨论】:

        【解决方案4】:

        session.close() 只负责关闭会话 https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#close()

        如何使用休眠会话: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-basics

        // Non-managed environment idiom
        Session sess = factory.openSession();
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
        
            // do some work
            ...
        
            tx.commit();
        }
        
        catch (RuntimeException e) {
            if (tx != null) tx.rollback();
            throw e; // or display error message
        }
        finally {
            sess.close();
        }
        

        正如它在文档中所写: 一个更灵活的解决方案是 Hibernate 内置的“当前会话”上下文管理:

        // Non-managed environment idiom with getCurrentSession()
        try {
            factory.getCurrentSession().beginTransaction();
        
            // do some work
            ...
        
            factory.getCurrentSession().getTransaction().commit();
        }
        catch (RuntimeException e) {
            factory.getCurrentSession().getTransaction().rollback();
            throw e; // or display error message
        }
        

        【讨论】:

          【解决方案5】:

          每当您对数据库对象执行任何工作单元时,都必须使用事务。 http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html 显示了使用它们的原因以及如何使用它们。但是,关键是通过调用返回Transaction 对象的session.beginTransaction() 来使用transaction 对象。这将代表对数据库执行的工作单元。

          【讨论】:

            【解决方案6】:

            我发现我必须先刷新然后刷新 DAO,然后它才能工作。

            session.flush()
            session.refresh(entityDAO)
            

            【讨论】:

              【解决方案7】:

              你忘记提交了。你应该提交。

              session.save(ac);
              // you should to add this bottom line  
              session.getTransaction().commit();
              

              【讨论】:

                【解决方案8】:

                每当你在做 save() 时,总是在完成后提交事务。

                Account ac =new account()
                ac.insert(123);
                ----------
                ----------
                ----------
                ----------
                session.save(ac);
                
                // Add this bottom line  
                session.getTransaction().commit();
                

                【讨论】:

                  【解决方案9】:
                  if there is DB manipulation operation then it should be in transaction
                  if you call session.save(object)
                  hibernate create bellow query only .
                  select hibernate_sequence.nextval from dual
                  so right code is here
                  Transaction  t=session.beginTransaction();
                  session.save(obect);
                  t.commit(); should not forgot to commit.
                  session.close();
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2018-06-09
                    • 2019-11-03
                    • 2023-01-04
                    • 2020-05-03
                    • 2015-03-16
                    • 2010-11-04
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多