做批量更新时,PostUpdate挂钩不起作用 [英] PostUpdate hook is not working, while doing batch update

查看:66
本文介绍了做批量更新时,PostUpdate挂钩不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用JPA/Hibernate.

I am working with JPA/Hibernate.

我正在批量更新:

I am doing bulk-update:

代码段:

@PersistenceContext
private EntityManage entityManager;

   ...
private int update(StudentEntity studentEntity){
 CriteriaBuilder builder = entityManager.getCriteriaBuilder();
 CriteriaUpdate<StudentEntity > criteria = builder.createCriteriaUpdate(StudentEntity.class);
 Root<StudentEntity> root = criteria.form(StudentEntity.class)
 setFields(criteria, root, studentEntity)
 criteria.where(builder.equal(root.get("studentId"), studentEntity.getStudentId()));
 return entityManager.createQuery(criteria).executeUpdate();
}

private setFields(CriteriaUpdate<StudentEntity> criteria,  Root<StudentEntity> root,StudentEntity  studentEntity){
 criteria.set(root.get("studentName"),studentEntity.getStudentName());

 ....
}


执行上面的代码@PostUpdate挂钩时没有被调用,为什么?

While executing the above code @PostUpdate hook is not getting invoked, why?

@PostUpdate
private update (StudentEntity studentEntity){
  System.out.println("@PostUpdate called.");
}


推荐答案

根据JPA规范(第3.5.3节):

From the JPA specification (section 3.5.3):

PreUpdatePostUpdate回调分别发生在对实体数据进行数据库更新操作之前和之后.

The PreUpdate and PostUpdate callbacks occur before and after the database update operations to entity data respectively.

即这些事件仅在使用实体生命周期加载实体,对其进行处理然后将状态刷新到数据库时触发.

I.e. these events only get triggered when using the entity lifecycle to load an entity, manipulate it and then flush the state to the database.

这篇关于做批量更新时,PostUpdate挂钩不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆