PostUpdate hook is not working, while doing batch update

梦想与她 提交于 2020-04-18 05:47:16

问题



I am working with JPA/Hibernate.

I am doing bulk-update:

Code Snippet:

@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());

 ....
}


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

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



回答1:


From the JPA specification (section 3.5.3):

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.



来源:https://stackoverflow.com/questions/61227657/postupdate-hook-is-not-working-while-doing-batch-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!