java - Hibernate AUTO 刷新策略顺序

标签 java spring hibernate transactions flush

我有 2 种方法在 Hibernate 中表现出不同的刷新行为。

第一个是:

    @Transactional
    public void firstMthod(int id, int status) {
        Person entity = session.get(Person.class, id);
        entity .setStatus(personStatus.registered);
        session.merge(entity);
        updatePersonAge(id,18);
    }

updatePersonAge 方法位于另一个类中,该方法的 SQL 输出如下所示:

select personel0_.ID            as ID1_119_0_,
       personel0_.status        as status2_119_0_,
       personel0_.age           as age3_119_0_,
       personel0_.CreatedBy     as CreatedBy4_119_0_,
       personel0_.UpdatedBy     as UpdatedBy5_119_0_,
       personel0_.CreatedDate   as CreatedDate6_119_0_,
       personel0_.UpdatedDate   as UpdatedDate7_119_0_,
       personel0_.Ip            as Ip8_119_0_
  from tbl_personel personel0_
 where personel0_.ID = ?
update tbl_person set status = ? where ID = ?
update tbl_person set age = ? where ID = ?

对于第二个用例,我们有以下方法:

    @Override
    @Transactional
    public void secondMethod(int id,int courseId, int status) {
        Course courseEntity=session.get(Course .class, courseId); 
        courseEntity.setCreatedDate(new Date());
         session.merge(courseEntity);
        updatePersonAge(id,18);
    }

为此 updatePersonAge 方法生成以下 SQL 输出:

select course0_.ID          as ID1_120_0_,
       course0_.CreatedBy   as CreatedBy7_120_0_,
       course0_.UpdatedBy   as UpdatedBy8_120_0_,
       course0_.CreatedDate as CreatedDate9_120_0_,
       course0_.UpdatedDate as UpdatedDate10_120_0_,
       course0_.Ip          as Ip11_120_0_
  from tbl_course course0_
 where course0_.ID = ?
update tbl_course set created_date = ? where ID = ?
update tbl_person set age = ? where ID = ?

updatePersoneAge 方法是:

public int updatePersonAge(int id,int age){
        Query query = session.createQuery("update " + domainClass.getName() + " e set e.age= :age ");
                query.setParameter("age ", age);
                return query.executeUpdate();
        }

按照我的预期,第二种方法的输出应该与第一种方法的输出相同。那么为什么不同呢?这真的很困惑。

最佳答案

首先,调用merge是没有意义的在已经附加到当前运行的 Session 的实体上。 Merge 用于附加分离的实体。

二、 hibernate FetchMode.AUTO如果要运行的查询与 ActionQueue 中的实体重叠,flush 只会触发刷新。

在第一个示例中,因为您修改了 Person,并且查询是针对 Person 运行的,所以触发刷新是有意义的,否则,SQL 查询可能会返回过时的结果。

在第二种情况下,您修改了一个Course 实体,但您想要从Person 中进行选择。因此,无需触发冲洗。

您可以使用 Query.addSyncronizedEntityName 控制此行为.

关于java - Hibernate AUTO 刷新策略顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42343661/

相关文章:

java - 如何使用java将文件从Windows计算机发送(移动/复制)到服务器

java - flashAttributes 在 websphere 重定向期间丢失

java - spring 中@Value 的插入顺序不明确

hibernate - Spring boot、JPA、Hibernate 和 H2 DB 测试 - 序列不起作用 - 意外的代码路径”;SQL 语句 : call next value for seq_my_jobs [50000-193]

java - hibernate DAO 错误

java - 关于如何组合CDI beans的疑惑: SessionScoped and RequestScoped

java - 界面设计问题: Storing Result of Transactions

Spring Data Solr @Transaction 提交

java - 具有 Hibernate/Spring Boot 字段集合的 Spring DTO 的单元测试

java - JNI C++ DLL - 'UnsatisfiedLinkError: %1 is not a valid Win32 application'