JPA孤儿删除不适用于OneToOne关系 [英] JPA orphan removal does not work for OneToOne relations

查看:198
本文介绍了JPA孤儿删除不适用于OneToOne关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有解决此问题的方法: https://hibernate.atlassian.net/浏览/ HHH-9663

我也面临类似的问题。当我在两个实体之间创建单向(无反向引用)一对一关系并将orphan removal属性设置为true时,在将引用设置为null之后,引用的对象仍在数据库中。



以下是示例域模型:

  @Entity 
public class Parent {
$ b @OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL,orphanRemoval = true)
@JoinColumn(name =child_id)
私人孩子孩子;
...
}

@实体
公共类子元素{
...
@Lob
私有字节[ ]数据;
...
}

目前我正在通过手动删除孤儿。

解决方案

rel =nofollow noreferrer从父传播到的实体状态转换/rel =nofollow noreferrer>实体状态转换。在您的情况下,父母实际上是该协会的子女(拥有FK)。



尝试使用此映射:

  @Entity 
public class Parent {
...
@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL, orphanRemoval = true,mappedBy =parent)
私人孩子孩子;
...
}

@Entity
public class Child {

@OneToOne
@JoinColumn(name = parent_id)
私人父母;

...
@Lob
private byte [] data;
...
}

为了级联孤儿去除,你现在需要:

 父亲= ...; 
parent.getChild()。setParent(null);
parent.setChild(null);


Does anyone have a workaround for this issue: https://hibernate.atlassian.net/browse/HHH-9663?

I am also facing a similar issue. When I created one-sided (no reverse reference) one to one relationship between two entities and set the orphan removal attribute to true, the referenced object is still in the database after setting the reference to null.

Here is the sample domain model:

@Entity
public class Parent {
  ...
  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name = "child_id")
  private Child child;
  ...
}

@Entity
public class Child {
  ...
  @Lob
   private byte[] data;
  ...
}

I am currently working around this by manually deleting orphans.

解决方案

Cascading only makes sense for entity state transitions that propagate from a Parent to a Child. In your case, the Parent was actually the child of this association (having the FK).

Try with this mapping instead:

@Entity
public class Parent {
  ...
  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent")
  private Child child;
  ...
}

@Entity
public class Child {

    @OneToOne
    @JoinColumn(name = "parent_id")
    private Parent parent;

    ...
    @Lob
    private byte[] data;
    ...
}

And to cascade the orphan removal, you now need to:

Parent parent = ...;
parent.getChild().setParent(null);
parent.setChild(null);

这篇关于JPA孤儿删除不适用于OneToOne关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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