休眠:从列表中删除项目不会持续 [英] Hibernate: Removing item from a List does not persist
问题描述
从列表中删除项目时遇到问题。该列表在超类中定义,但Hibernate注释应用于子类中的属性访问器。超类中有两种方法可以操纵列表。 添加方法工作正常,但删除不会持续更改。我检查了我的Cascade设置,并且我似乎有些事情是正确的。我在做一些不可能的事情吗?如果不是,我是否做了不正确的事情?
以下是我的课程:
@Entity
抽象类Temporal< T> {
@Id
@GeneratedValue
私人长ID;
@版本
私人整数版本=空;
@Transient
受保护的列表< T> content = new ArrayList< T>();
$ b $ public void remove(T value){
//商业逻辑...
content.remove(value);
}
public void add(T value){
//商业逻辑...
content.add(value);
}
}
@Entity
@AccessType(property)
class TemporalAsset extends Temporal< Asset> {
@OneToMany(cascade = CascadeType.ALL,mappedBy =temporal)
public List< Asset> getContent(){
return super.content;
}
protected void setContent(List< Asset> list){
super.content = list;
$ b我使用TemporalAsset类的一个实例,如下所示(请注意,我只使用刷新方法来演示行为。即使我刷新或关闭会话并打开新会话,列表也不会正确保存): temporalAsset.add(值1);
temporalAsset.getContent()。size()== 1; // true
session.update(temporalAsset);
session.refresh(temporalAsset);
temporalAsset.getContent()。size()== 1; // true
temporalAsset.remove(value1);
temporalAsset.getContent()。size()== 0; // true
session.update(temporalAsset);
session.refresh(temporalAsset);
temporalAsset.getContent()。size()== 0; // false,它的1
谢谢。
尝试将代码更改为
@OneToMany
@Cascade(cascade = {CascadeType.ALL,CascadeType.DELETE_ORPHAN},mappedBy =temporal)
部分来自hibernate docs :
如果子对象的寿命是
有界通过父
对象的生命周期,通过指定
CascadeType.ALL和
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
使父项成为完整的
生命周期对象(请参阅Hibernate
参考指南,了解
孤立删除的语义)
I am having trouble when removing an item from a list. The list is defined in a superclass, but the Hibernate annotations are applied to property accessors in a subclass. There are two methods in the superclass that manipulate the list. The "add" method works fine, but the "remove" does not persist changes. I have checked my Cascade settings, and I seem to have things correct. Am I doing something that is impossible. If not, am I doing something incorrectly?
Here are my classes:
@Entity
abstract class Temporal<T> {
@Id
@GeneratedValue
private Long id;
@Version
private Integer version = null;
@Transient
protected List<T> content = new ArrayList<T>();
public void remove(T value) {
// business logic ...
content.remove(value);
}
public void add(T value) {
// business logic ...
content.add(value);
}
}
@Entity
@AccessType("property")
class TemporalAsset extends Temporal<Asset> {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "temporal")
public List<Asset> getContent() {
return super.content;
}
protected void setContent(List<Asset> list) {
super.content = list;
}
}
I use an instance of the TemporalAsset class as follows (note that I am only use the "refresh" method to demonstrate the behavior. The list does not persist correctly even if I flush or close the session and open a new session):
temporalAsset.add(value1);
temporalAsset.getContent().size() == 1; // true
session.update(temporalAsset);
session.refresh(temporalAsset);
temporalAsset.getContent().size() == 1; // true
temporalAsset.remove(value1);
temporalAsset.getContent().size() == 0; // true
session.update(temporalAsset);
session.refresh(temporalAsset);
temporalAsset.getContent().size() == 0; // false, its 1
Thanks.
解决方案 You have to explicitly specify cascade as CascadeType.DELETE_ORPHAN.
Try to change code to
@OneToMany
@Cascade(cascade = {CascadeType.ALL, CascadeType.DELETE_ORPHAN}, mappedBy = "temporal")
Part from hibernate docs:
If the child object's lifespan is
bounded by the lifespan of the parent
object, make the parent a full
lifecycle object by specifying
CascadeType.ALL and
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
(please refer to the Hibernate
reference guide for the semantics of
orphan delete)
这篇关于休眠:从列表中删除项目不会持续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文