添加到集合时,PreUpdate不会触发 [英] PreUpdate not firing when adding to a collection
问题描述
@Entity
public class Employee {
@Id
private int id;
@Basic
私人字符串名称;
@OneToMany
@JoinTable(name =ORG,joinColumns = @JoinColumn(name =MINION),
inverseJoinColumns = @JoinColumn(name =EMP))
私人列表<员工> minions = new ArrayList< Employee>();
@PreUpdate
public void preUpdate(){...}
}
我所看到的是,如果我有一个托管的Employee实体并添加到它的小爪子集合中,则不会调用 preUpdate
方法。新的行被添加到数据库中的映射表中,所以我知道更新正在进行。如果我直接在员工上更改属性,如名称,那么在事务提交时按预期触发 preUpdate
。
<有没有一种方法可以在映射集合被修改时获取PreUpdate?或者是否有其他技术或Hibernate特定的注释来检测这种情况何时发生?
@PreUpdate
事件在数据库UPDATE操作是针对有问题的实体执行的。
如果您未更新 Employee $ c的直接属性$ c>,没有
UPDATE
来执行它的表,因此从不调用 @PreUpdate
侦听器。您应该使用 @PrePersist
事件来获得更好的运气,这是由flush而不是update触发的。
I have a JPA annotated class which contains a collection like so:
@Entity
public class Employee {
@Id
private int id;
@Basic
private String name;
@OneToMany
@JoinTable(name = "ORG", joinColumns = @JoinColumn(name="MINION"),
inverseJoinColumns = @JoinColumn(name="EMP"))
private List<Employee> minions = new ArrayList<Employee>();
@PreUpdate
public void preUpdate(){ ... }
}
What I'm seeing is that if I have a managed Employee entity and I add to it's collection of minions the preUpdate
method is not getting invoked. A new row is added to the mapping table in the DB so I know the update is going through. If I change a property directly on the Employee, like name, then preUpdate
fires as expected when the transaction is committed.
Is there a way to get PreUpdate to fire when a mapped collection is modified? Or is there some other technique or Hibernate specific annotation for detecting when this happens?
@PreUpdate
event is triggered just before database UPDATE operation is executed for the entity in question.
If you're not updating direct properties of Employee
, there's no UPDATE
to execute for its table and thus @PreUpdate
listener is never called. You should have better luck using @PrePersist
event which is triggered by "flush" rather than "update".
这篇关于添加到集合时,PreUpdate不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!