简体   繁体   English

当我对实体 object 执行 saveAndFlush 时,我看到 hibernate 生成 2 个更新查询。 为什么会这样以及如何避免?

[英]When I do saveAndFlush on an entity object, I see hibernate generates 2 update queries. Why is it and how to avoid it?

These are the steps I am doing - 1.Load the entity 2.update few fields on the entity.这些是我正在做的步骤 - 1.加载实体 2.更新实体上的几个字段。 3.saveAndFlush 3.saveAndFlush

I see one update query with all 3 fields updated in Step#2.我看到一个更新查询,在步骤#2 中更新了所有 3 个字段。 Other update query is updating one of the 3 fields but the value is from Step#1 and not Step#2.其他更新查询正在更新 3 个字段之一,但该值来自 Step#1 而不是 Step#2。

Can someone please help me figure out how to avoid that?有人可以帮我弄清楚如何避免这种情况吗?

EDIT- Added my classes.编辑-添加了我的课程。

@Entity
@DynamicUpdate
public class Enrollment
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Embedded
    private Section sectionInformation = new Section();

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "student_id")
    private Student student;

    @Enumerated(EnumType.STRING)
    private TakingExam takingExam;
}
public ResponseEntity controllerMethod(){
     Enrollment enrollment = service.getEnrollment(enrollmentId);
        if (enrollment == null)
        {
            return new ResponseEntity<>(NOT_FOUND);
        }
        
        Enrollment otherEnrollment = null;
        if (check some condition)
        {
            otherEnrollment = 
               service.getEnrollment(request.someValue());
            validator.validateOtherEnrollment(enrollment, 
               otherEnrollment, request.someValue());
        }
       enrollment.takingExam(request.getTakingExam());
        if (otherEnrollment != null)
        {
            service.swapEnrollments(enrollment, otherEnrollment);
        }
}
private void swapEnrollments(Enrollment enrollment, String userName){
///lots of validations
    enrollment.setUpdatedBy(userName);
        fromEnrollment.setUpdatedDate(new 
    Timestamp(System.currentTimeMillis()));
    enrollmentRepo.saveAndFlush(enrollment); ---- THIS IS WHERE I SEE 2 UPDATE QUERIES AS PUT BELOW
}
update
        enrollment 
    set
        taking_exam=?,
        updated_by=?,
        updated_date=? 
    where
        id=?

 update
        enrollment 
    set
        taking_exam=? 
    where

This is what is happening in my code -这就是我的代码中发生的事情 -

  1. Load the entity 1加载实体 1
  2. Load the entity 2加载实体 2
  3. update few fields on the entity 1.更新实体 1 上的几个字段。
  4. update few fields on entity 2更新实体 2 上的几个字段
  5. saveAndFlush entity 1- This step was creating 2 update queries. saveAndFlush 实体 1- 这一步是创建 2 个更新查询。

This is what helped avoid intermediate auto flush这有助于避免中间自动刷新

  1. Load the entity 1加载实体 1
  2. Load the entity 2加载实体 2
  3. update few fields on the entity 1.更新实体 1 上的几个字段。
  4. saveAndFlush entity 1 -- Only 1 update query generated !! saveAndFlush 实体 1 - 仅生成 1 个更新查询!
  5. update few fields on entity 2更新实体 2 上的几个字段
  6. saveAndFlush entity 2 saveAndFlush 实体 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么冬眠在执行saveAndFlush之前再次加载实体? - Why is hibernate loading an entity again before doing saveAndFlush? 我什么时候会看到休眠实体的更新? - When do I see updates on hibernate entities? 当我删除@OneToMany 关系中的实体时,Hibernate,n+1 查询 - Hibernate, n+1 queries when i delete an entity in a @OneToMany relation 如何避免将与字段关联的对象列表检索到Hibernate实体类中? - How can I avoid to retrieve the list of object associated as field into an Hibernate entity class? 当我在同一个对象/表上使用 HQL 进行读取时,hibernate 如何执行更新 sql 语句? - How come hibernate executes update sql statements when I do a read using HQL on the same object/table? 如何避免Hibernate ValidationException上的自动实体更新 - How to avoid automatic entity update on Hibernate ValidationException 当链接实体名称不是从父实体名称衍生而来时,如何避免OneToOne映射出现NullPointerException? - How do I avoid NullPointerException with OneToOne mapping when linked entity name not derived from parent entity name? 发送对象时如何避免每个套接字超时? - How do I avoid the timeout of each socket when sending an object? 为什么休眠会生成约500个SQL查询? - Why hibernate generates ~500 SQL queries? 在Hibernate中,当对象的字段被延迟加载时,我只能更改其中一个字段并执行update(object)吗? - In Hibernate, when an object's fields are lazy loaded, can I only change one of the fields and do update(object)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM