Java JPA + Hibernate + Spring + OneToMany 删除级联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25967935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA + Hibernate + Spring + OneToMany delete cascade
提问by Garci García
I've read some related questions but they are not exactly the same problem as mine.
我已经阅读了一些相关的问题,但它们与我的问题并不完全相同。
I'm using JPA + Hibernate + Spring and I want to do something that I'm not sure if it is possible just with config.
我正在使用 JPA + Hibernate + Spring,我想做一些我不确定仅使用 config 是否可行的事情。
I have my domain classes with a more or less complicated relation. There are many elements that are related with one element (like if it was a tree many elements are sons of one element).
我的域类或多或少具有复杂的关系。有许多元素与一个元素相关(就像一棵树,许多元素都是一个元素的儿子)。
Something like:
就像是:
@Entity
class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "PARENT_ID")
private Foo parentNode;
...
}
Which will get a table like:
这将得到一个表,如:
Foo id parent_id
1
2 1
3 1
When I delete row with id = 1 I want to delete rows with id = 2 and id = 3 (it may be recursive, elements with parent_id = 2 and parent_id = 3 would be deleted as well).
当我删除 id = 1 的行时,我想删除 id = 2 和 id = 3 的行(它可能是递归的,parent_id = 2 和 parent_id = 3 的元素也会被删除)。
For some restrictions I only can have the relation in son's side with the parent_id reference.
对于某些限制,我只能在儿子身边与 parent_id 参考建立关系。
My question is: is it possible to do this with JPA or Hibernate configuration or do I need to do some recursive function to delete all children and all parents?
我的问题是:是否可以使用 JPA 或 Hibernate 配置来执行此操作,或者我是否需要执行一些递归函数来删除所有子项和所有父项?
I've tried with:
我试过:
@OneToMany(name = "PARENT_ID", cascade = CascadeType.REMOVE)
And I've read that maybe using Hibernate annotations.
我读过可能使用 Hibernate 注释。
If anyone can give me some clue I'm lost at this point.
如果有人能给我一些线索,我在这一点上迷路了。
Edit 1
编辑 1
Would it be possible to do like:
是否可以这样做:
@Entity
class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name="PARENT_ID")
private Foo parentNode;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parentNode", cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Foo> childs = new LinkedHashSet<Foo>();
...
}
Keeping the table as is, with the fk to the parent? I've tried this but I keep getting the same error, fk restriction violated.
保持表格不变,将 fk 交给父级?我试过这个,但我一直收到同样的错误,违反了 fk 限制。
Edit 2
编辑 2
Finally solved with:
最后解决了:
@Entity
class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "PARENT_ID")
private Foo parentNode;
@OneToMany(mappedBy = "parentNode", cascade = CascadeType.REMOVE)
private Set<Foo> childs = new LinkedHashSet<Foo>();
...
}
This @OneToMany
is needed even if we do the mapping in our BBDD by refering just the parent id.
@OneToMany
即使我们在 BBDD 中仅通过引用父 ID 进行映射,这也是必需的。
Now when we delete a Foo with childs, it's childs will be deleted as well.
现在,当我们删除带有孩子的 Foo 时,它的孩子也将被删除。
Thanks for your time and good advices!
感谢您的时间和好的建议!
采纳答案by Harshal Patil
Relationships in JPA are always unidirectional, unless you associate the parent with the child in both directions. Cascading REMOVE operations from the parent to the child will require a relation from the parent to the child (not just the opposite).
JPA 中的关系始终是单向的,除非您将父级与子级双向关联。从父级到子级的级联 REMOVE 操作将需要从父级到子级的关系(不仅仅是相反的)。
So here you need to change unidirectional relationship to bi-directional.
所以这里需要把单向关系改成双向关系。
for more details refer this link.
有关更多详细信息,请参阅此链接。
回答by jmvivo
Look at orphanRemovaloption:
查看orphanRemoval选项:
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
Here is complete explicationabout CascadeType.REMOVE
and orphanRemoval
.
这是关于和 的完整说明。CascadeType.REMOVE
orphanRemoval
Good luck!
祝你好运!