Hibernate + Spring LocalSessionFactory更新查询正确执行,但数据库中没有保存更改

时间:2018-04-02 07:35:30

标签: java hibernate spring-mvc

package com.kk.mypackage;
@Repository
@Transactional public class DBConnectionCredit implements DBConnectionCreditInt {

    @Autowired
    private LocalSessionFactoryBean sessionFactory;

    @Override
    public void insertEmailSaveRequest(EmailSaveRequest emailSaveRequest)
    {
        try
        {
            StringBuilder sbCover=new StringBuilder();
            sbCover.append("UPDATE QCIB_EMAIL_SAVE_REQ SET ");
            sbCover.append("CORELATIONID='12345' ");
            sbCover.append(" WHERE SID='412'");


            int i=sessionFactory.getObject().getCurrentSession().createSQLQuery(sbCover.toString()).executeUpdate();

            System.out.println(" i= "+i+" data saved : "+sbCover);

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

控制台:

Hibernate: UPDATE QCIB_EMAIL_SAVE_REQ SET CORELATIONID='12345'  WHERE SID='412'

i= 0 data saved : UPDATE QCIB_EMAIL_SAVE_REQ SET CORELATIONID='12345'  WHERE SID='412'

1 个答案:

答案 0 :(得分:-1)

  1. 您应该检查会话是否已打开。我猜你没有开会。
  2. 您可以自动装配EntityManager,然后解开Session对象。