jpa从列表中删除项目 [英] jpa removing item from list

查看:114
本文介绍了jpa从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有1-N关系(对象有一个列表),并希望从列表中删除一项.用JPA删除列表中项目的正确方法是什么?

I have 1-N relationship (object has a list) and want remove one item from the list. What is the right way of removing item in the list with JPA?

我是否必须先更新列表并合并所有者,然后再删除项目(如果我不删除项目而只是合并所有者,那么孤儿将保留在数据库中,对)?应该以什么顺序调用merge()和remove(),remove()和merge()的顺序重要吗?

Do I have to first update the list and merge the owner and remove the item after (if I do not remove the item and just merge the owner, orphan will remain in DB, right)? In what sequence should be merge() and remove() called, does the order of remove() and merge() matter?

代码示例:

List<Item> items = owner.getItems();
Item itemToRemove = items.get(index);
owner.remove(itemToRemove);
em.merge(owner);
em.remove(itemToRemove);

推荐答案

如果您拥有orphanRemoval=true(JPA 2.0),这应该可以工作.

This should work in case you have orphanRemoval=true (JPA 2.0).

对于JPA 1,没有标准的方法来处理此问题. Hibernate具有@Cascade,您可以在其中指定DELETE_ORPHAN

For JPA 1 there is no standard way to handle this. Hibernate has @Cascade where you can specify DELETE_ORPHAN

这篇关于jpa从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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