问题
I have a one to many relationship between event and session. I'd like cascade to update event fk in session to null when i delete the corresponding event. Any clue how to do this? Thanks and advance.
回答1:
Hibernate or JPA unfortunately don't have a Cascade type 'SET to NULL' but you should be able to do it with @PreRemove
on the one side (Owner):
@OneToMany(mappedBy="whatever")
public List<SomeEntity> getSomeEntity(){
return someEntity;
}
@PreRemove
public void onDelete(){
for(SomeEntity se : getSomeEntity()){
se.setOwner(null);
}
}
Hope that helps.
来源:https://stackoverflow.com/questions/4802863/hibernate-cascade-update-child-to-null