JPA @PreUpdate for update query using entityManager.createNa



我有以下基本实体类,它由 2 个 JPA 实体类扩展。

@MappedSuperclass
public class BaseEntity implements Serializable {
...
...
@PrePersist
protected void onCreate() {
this.modifiedOn = new Date();
}
@PreUpdate
protected void onUpdate() {
this.modifiedOn = new Date();
}
}

现在,如果我使用实体管理器使用以下本机查询调用更新几列。它会打电话给@PreUpdate吗?

Query query = super.getEntityManager(Constant.PSUNIT).createNativeQuery("UPDATE CUSTOMER SET TYPE = 'XYZ' WHERE SOURCE = 'ABC'");
query.executeUpdate();

@Entity
@Table(name = "CUSTOMER")
public class Customer extends CustomerSuper {
// customer related fields. and getting setter with annotations.
...
... 
...
}

@MappedSuperclass
public class CustomerSuper extends BaseEntity implements Serializable {

尽管我们使用的是实体管理器,但如果为本机查询触发更新,看起来它不会调用 preUpdate。

这背后的原因可能是创建查询的工作由我们的代码管理,而不是休眠。

最新更新