问题
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
andPostUpdate
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