【发布时间】:2018-01-25 23:48:35
【问题描述】:
我们有两个 Spring Data JPA 实体(父项,子项),字段设置为特定值的子项的计数会影响在@PostLoad 期间设置的@Transient 属性中父项记录的值
家长:
@Entity
public class Parent {
@Transient
private boolean status = false;
@OneToMany
@Where("STATUS = true")
private Set<Children> childrens;
@PostLoad
public void postload(){
if(childrens.size() > 0) this.status = true;
}
....
}
孩子们:
@Entity
@EntityListeners({ ParentListener.class })
public class children {
private Boolean status = false;
@ManyToOne
private Parent parent;
}
在我的控制器/服务类中(NOT注释为@Transactional,我来更新Children记录的状态值:
@Service
public class ChildrenService {
...
public void doStuff(Children child) {
child.status = true;
childRepository.save(child);
}
}
现在ParentListener 启动了,我想在父母的状态值发生变化时记录。
class ParentListener {
@PostUpdate // AFTER Children record updated
public void childPostPersist(Children child) {
AutowireHelper.autowire(this);
// here child.parent.status == false (original value)
// but I have set this child record equal to true, and
// have triggered the `save` method, but assuming I am still
// in the session transaction or flush phase, the
// parent related record's status is not updated?
System.out.println(child.parent.status); // prints false
Parent currentParent = parentRepository.getOne(child.parent.getId());
System.out.println(currentParent.status); // prints false
}
}
我对 @Transactional、@Postload 和 transactions/sessions 和 EntityListeners 有什么误解?
PS。 AutowireHelper 引用自here
【问题讨论】:
标签: hibernate jpa spring-data spring-data-jpa entitylisteners