hibernate cascade - update child to null

一笑奈何 提交于 2019-12-11 03:39:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!