Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在JPA2.1中执行批量更新、删除和插入时调用回调方法,如@PreUpdate、@PrePersist、@PreRemove?_Hibernate_Jpa_Eclipselink_Criteria_Jpa 2.1 - Fatal编程技术网

Hibernate 如何在JPA2.1中执行批量更新、删除和插入时调用回调方法,如@PreUpdate、@PrePersist、@PreRemove?

Hibernate 如何在JPA2.1中执行批量更新、删除和插入时调用回调方法,如@PreUpdate、@PrePersist、@PreRemove?,hibernate,jpa,eclipselink,criteria,jpa-2.1,Hibernate,Jpa,Eclipselink,Criteria,Jpa 2.1,如何调用JPA2.1中由@PreUpdate注释的方法(包括@PrePersist、@PreRemove等)?以以下CriteriaUpdatequery为例: Brand brand=//... Populated by client. The client is JSF in this case. byte[] bytes=//... Populated by client. The client is JSF in this case. CriteriaBuilder criteriaB

如何调用JPA2.1中由
@PreUpdate
注释的方法(包括
@PrePersist
@PreRemove
等)?以以下
CriteriaUpdate
query为例:

Brand brand=//... Populated by client. The client is JSF in this case.
byte[] bytes=//... Populated by client. The client is JSF in this case.

CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaUpdate<Brand>criteriaUpdate=criteriaBuilder.createCriteriaUpdate(Brand.class);
Root<Brand> root = criteriaUpdate.from(entityManager.getMetamodel().entity(Brand.class));

criteriaUpdate.set(root.get(Brand_.brandName), brand.getBrandName());
criteriaUpdate.set(root.get(Brand_.category), brand.getCategory());
criteriaUpdate.set(root.get(Brand_.brandImage), bytes);
criteriaUpdate.where(criteriaBuilder.equal(root, brand));
entityManager.createQuery(criteriaUpdate).executeUpdate();
此方法仅在使用更新行时调用

entityManager.merge(brand);

如何调用由
@PreUpdate
(或
@PrePersist
@PreRemove
)修饰的方法,当相关操作涉及标准API时,如
CriteraUpdate

您没有将实体传递给JPA。您只是提取单个实体属性并将其传递给JPA。为了获得实体的
@PreUpdate
和JPA调用的好友,您需要将整个实体传递给JPA,就像使用
EntityManager\merge()一样

如果这是一个不明确的原因的选项(也许实体太大了,你想跳过不必要的属性而不被更新?在这种情况下,考虑把实体分割成更小的<代码> @ OnEthONo./COD>关系),然后,在提取实体的属性并将其传递给JPA之前,您需要手动调用实体上的这些方法

Brand brand = ...
brand.onUpdate();
byte[] bytes = ...

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
// ...

你不能。Criteria/JPQL批量更新/删除不会对JPA实体进行操作,因此它们无法触发实体回调方法。您需要查询实体,然后直接进行修改以调用回调。或者将回调合并到批量更新/删除语句中我没有得到这条语句,“或者将回调合并到批量更新/删除语句中”@克里斯克里斯是对的。include意味着在条件查询中包含@PreUpdate Java方法中的逻辑(比如设置一些附加字段)。
Brand brand = ...
brand.onUpdate();
byte[] bytes = ...

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
// ...