休眠级联 - 更新孩子为空 [英] hibernate cascade - update child to null

查看:130
本文介绍了休眠级联 - 更新孩子为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在事件和会话之间有一对多的关系。我想级联更新会话中的事件fk为空,当我删除相应的事件。任何线索如何做到这一点?
Thanks and advance。

解决方案

Hibernate或JPA不幸没有Cascade类型'SET为NULL',但你应该可以通过@PreRemove



在一方(所有者)上做到这一点:

  @OneToMany(mappedBy =whatever)
public List< SomeEntity> getSomeEntity(){
返回someEntity;


@PreRemove
public void onDelete(){
for(SomeEntity se:getSomeEntity()){
se.setOwner(null);
}
}

希望有帮助。

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.

解决方案

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.

这篇关于休眠级联 - 更新孩子为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆