hibernate session.save() does not reflect in database

后端 未结 9 2119
臣服心动
臣服心动 2020-12-08 14:30

According to my understanding in hibernate (please confirm)

1- You have to session.close() if you get it by getSessionFactory().openSession()

相关标签:
9条回答
  • 2020-12-08 14:57

    Whenever you carry out any unit of work on the database objects, Transactions have to be used. http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html shows why they are used and how they can be used. But, the key is to use a transaction object by calling session.beginTransaction() which returns a Transaction object. This will represent the unit of work carried out to the database.

    0 讨论(0)
  • 2020-12-08 14:59

    When you create session using SessionFactory.openSession(), no transaction is created, so your operations are executed outside of transaction context. In order to see your changes, you have to start a new transaction, or perform your operations as a part of ongoing transaction. From documentation:

    A typical transaction should use the following idiom:

    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();
     }
    
    0 讨论(0)
  • 2020-12-08 15:04

    You forgot to commit .You should to commit.

    session.save(ac);
    // you should to add this bottom line  
    session.getTransaction().commit();
    
    0 讨论(0)
  • 2020-12-08 15:12

    Whenever Your are doing save(), Always commit the transaction once done.

    Account ac =new account()
    ac.insert(123);
    ----------
    ----------
    ----------
    ----------
    session.save(ac);
    
    // Add this bottom line  
    session.getTransaction().commit();
    
    0 讨论(0)
  • 2020-12-08 15:13

    Yeah session.save is not saved to database without have the transaction. Only way to do it is by using the

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

    If this property is used you no need transaction and also you do not need session.flush() or session.close()

    0 讨论(0)
  • 2020-12-08 15:14

    I found I had to flush and then refresh the DAO, and then it worked.

    session.flush()
    session.refresh(entityDAO)
    
    0 讨论(0)
提交回复
热议问题