将集合设置为“new ArrayList()”时级联休眠删除
我有一个使用 hibernate 映射的对象,其中包含 id 和集合。在某个时间点,当对象包含其集合中的项目时,我想要执行 obj.setCollection(new ArrayList()) 并让它从数据库中删除以前在集合中的项目。如果有人可以提供帮助,我们将不胜感激。说明问题的代码:
ORM: Foo 1-* Bar 和 Bar 1-1 Foo
class Foo {
Long id;
List<Bar> bars;
//getters & setters
}
<hibernate-mapping>
<class name="domain.Foo" table="Foos">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<list name="bars" lazy="true" cascade="all">
<key column="FOO_ID" not-null="true" />
<index column="SEQUENCE" />
<one-to-many class="domain.Bar" />
</list>
</class>
</hibernate-mapping>
@Entity
@Table(name="Bars")
public class Bar {
private Long id;
private Foo foo;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="FOO_ID", insertable=false, updatable=false)
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I have an object mapped with hibernate that contains an id and a collection. At some point in time when the object contains items in it's collection I want to do obj.setCollection(new ArrayList())
and have it delete the items from the database that were previously in the collection. If anyone could assist it would be much appreciated. Code to illustrate problem:
ORM: Foo 1-* Bar and Bar 1-1 Foo
class Foo {
Long id;
List<Bar> bars;
//getters & setters
}
<hibernate-mapping>
<class name="domain.Foo" table="Foos">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<list name="bars" lazy="true" cascade="all">
<key column="FOO_ID" not-null="true" />
<index column="SEQUENCE" />
<one-to-many class="domain.Bar" />
</list>
</class>
</hibernate-mapping>
@Entity
@Table(name="Bars")
public class Bar {
private Long id;
private Foo foo;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="FOO_ID", insertable=false, updatable=false)
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要做的是:
What you probably want to do is :
您永远不应该用新集合替换由 Hibernate 管理的集合(例如,如果您有一个包含集合的对象,并且该对象由 Hibernate 加载)!
始终使用集合方法(
add
、remove、
clear
...)来操作该方法。You should never replace a collection manged by Hibernate (for example if you have an object containing a collection, and this object is loaded by Hibernate) by an new collection!
Always use the collection methods (
add
,remove,
clear
, ...) to manipulate the method.如果您希望 Hibernate 在从集合中删除对象时从数据库中删除对象,则需要为该集合配置“delete-orphan”选项:
尽管在这种情况下您无法替换该集合,但您需要改为使用集合方法(请参阅拉尔夫的答案和 删除集合删除孤儿不适用于空分配?:()
If you want Hibernate to remove objects from the database when you remove them from the collection, you need to configure
delete-orphan
option for that collection:Though in this case you cannot replace the collection, you need to use collection methods instead (see Ralph's answer and remove collection with delete-orphan not work with null assignment? :()
或者类似的东西(我喜欢封装字段):
Or something like this (I like to encapsulate fields):