简体   繁体   中英

What I am missing here (Hibernate begin transaction)

I have a test class :

 class SomeTest {
       public static void main(String args[]){
       Client kom = new Client();
       kom.setId(kom.newID());
       kom.setClient("OldName");
       SessionFactory sessionFactory =  new    Configuration().configure().buildSessionFactory();
       Session session = sessionFactory.openSession();
       session.beginTransaction();
          session.save(kom); // I think insert is here ?
          Query q = session.createSQLQuery(" call change_name(:old, :new) ");
          q.setParameter("old","OldName");
          q.setParameter("new","NewName");
          int result = q.executeUpdate();
          session.getTransaction().commit();
        }
     };

And database stored procedure change_name which do update of name of client.. every time I run test I have one record with old name ?? I expect that update is execute in same transaction and that I never see old name ?

JB Nizet has right answer..

save() does NOT insert. It associates the object with the session. The insert query is executed at the next flush. So call flush explicitely before calling your stored proc. – JB Nizet yesterday

There is also FlushMode ... I set to ALWAYS. I call many procedures which change data in database..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM