JPA @PreUpdate 使用entityManager.createNativeQuery()进行更新查询

问题描述 投票:0回答:2

我有以下基本实体类,它由 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 {
hibernate jpa-2.0 hibernate-mapping
2个回答
0
投票

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

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


0
投票

这个问题你找到解决办法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.