【问题标题】:Hibernate executeUpdate is successful but list operation does not reflect changesHibernate executeUpdate 成功但列表操作未反映更改
【发布时间】:2017-05-17 08:27:00
【问题描述】:

我正在网上学习一个简单的 Hibernate 教程。似乎 executeUpdate 成功(返回的行数为 1)。但是,当我调用 list() 时,它返回了旧结果。这是休眠的预期行为吗?无论如何,我可以在同一个事务中获取更新的数据吗?

这是我的源代码:

//Prep work
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    //Get All Employees
    Transaction tx = session.beginTransaction();
    Query query = session.createQuery("from Employee");
    List<Employee> empList = query.list();
    for(Employee emp : empList){
        System.out.println("List of Employees::"+emp.getId()+","+emp.getAddress().getCity());
    }
    //Update Employee
    query = session.createQuery("update Employee set name= :name where id= :id");
    query.setParameter("name", "Pankaj Kumar");
    query.setLong("id", 1);
    int result = query.executeUpdate();
    System.out.println("Employee Update Status="+result);


    query = session.createQuery("from Employee");
    empList = query.list();
    for(Employee emp : empList){
        System.out.println("List of Employees::"+emp.getId()+","+emp.getAddress().getCity());
    }

    //rolling back to save the test data
    tx.rollback();

    //closing hibernate resources
    sessionFactory.close();

返回结果:

List of Employees::1,San Jose
List of Employees::2,Santa Clara
List of Employees::3,Bangalore
List of Employees::4,New Delhi
Hibernate: update EMPLOYEE set emp_name=? where emp_id=?
Employee Update Status=1
Hibernate: select employee0_.emp_id as emp_id1_1_, employee0_.emp_name as emp_name2_1_, employee0_.emp_salary as emp_sala3_1_ from EMPLOYEE employee0_
List of Employees::1,San Jose
List of Employees::2,Santa Clara
List of Employees::3,Bangalore
List of Employees::4,New Delhi

【问题讨论】:

    标签: java mysql hibernate


    【解决方案1】:

    事务提交后,更改实际上保存到数据库中,这就是事务的全部意义。

    如果您面临需要更新数据库上的数据的情况,然后将其获取以进行进一步处理,或者您在将驻留在持久性上下文中的对象上本地更新它(在这种情况下将更有意义) createOrUpdate 而不是您的查询),或将这两个步骤分开在 2 个事务中

    【讨论】:

      【解决方案2】:

      您再次执行相同的查询,但您从未提交过第一次事务,因此您所做的更改从未持久化。要解决此问题,您可以尝试提交 Hibernate 事务:

      tx.commit();
      

      您应该使用的模式如下所示:

      Transaction tx = session.beginTransaction();
      Query query = session.createQuery("from Employee");
      List<Employee> empList = query.list();
      // update, insert, etc.
      tx.commit();
      

      请注意,这应该发生在 try-catch 块中,如果发生异常,您将有机会回滚整个事务。

      【讨论】:

      • 即使更新成功,如何回滚事务?
      • @Tim Biegeleisen 为什么你在选择之后使用tx.commit();,我认为应该是在更新之后?
      • @XtremeBaumer 我在阅读 OP 时没有看到这个要求。你能详细说明你的评论吗?
      • 在他的代码中他得到了这个://rolling back to save the test data tx.rollback(); 这让我觉得他总是想在运行测试后恢复测试数据
      • 是的,但这与能够“看到”他的数据库中的数据相冲突。更新已提交或未提交,您不能同时拥有它。
      【解决方案3】:

      我假设您正在寻找测试您的 update 到 DB 和 rollback 一旦更新成功。如果是这样,您的交易中必须有savePoint。这篇Creating savepoint and rollingback in hibernate 的帖子可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 2021-01-12
        • 2018-12-17
        • 2014-07-12
        • 2015-03-06
        • 1970-01-01
        • 2018-12-05
        • 2015-04-19
        相关资源
        最近更新 更多