orphanRemoval 导致延迟加载集合出错
orphanRemoval causes error with lazy loaded collection
我使用 hibernate 5.0.8 和 spring data jpa 1.10.1
鉴于这些实体
class Model {
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(nullable = false)
private Configuration configuration;
//more fields and methods
}
class Configuration {
@OneToMany(mappedBy = "configuration", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Setting> settings = new ArrayList<>();
//more fields and methods
//settings is never assigned again - I use settings.add(...) and settings.clear()
}
class Setting {
@ManyToOne
@JoinColumn(nullable = false)
private Configuration configuration;
//more fields and methods
}
模型为主,但多个模型可以使用相同的配置。模型中配置的级联是必需的,因为如果我在配置中更改任何内容,我希望它应用到所有使用此配置的模型中
现在,当我使用具有设置的配置检索现有模型并保存此模型时,不对设置应用任何更改,我得到以下异常
@Transactional
public void doSomething() {
Model model = modelRepository.findOne(0);
//change something in the model, but no changes are made in its configuration
//or do nothing
modelRepository.save(model);
}
我得到以下异常
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Configuration.settings
我怀疑这与延迟加载的设置和休眠试图将空列表合并到配置中有关。
我做错了什么?
检查您在取消引用时尝试作为孤立对象清理的对象的 getter 和 setter:
尝试并使用:
public void setChildren(Set<Child> aSet) {
//this.child= aSet; //Results in this issue
//change to
this.child.clear();
if (aSet != null) {
this.child.addAll(aSet);
} }
问题是由使用 hibernate-enhance-maven-plugin 中的 enableLazyInitialization 引起的。我仍然不知道为什么会导致此错误,但删除此插件解决了问题。
我使用了这个插件,因为我想在模型中延迟加载一个大的字符串字段,我将在应用程序中缓存它。我现在将其更改为延迟获取的 OneToOne 关系。
我使用 hibernate 5.0.8 和 spring data jpa 1.10.1
鉴于这些实体
class Model {
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(nullable = false)
private Configuration configuration;
//more fields and methods
}
class Configuration {
@OneToMany(mappedBy = "configuration", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Setting> settings = new ArrayList<>();
//more fields and methods
//settings is never assigned again - I use settings.add(...) and settings.clear()
}
class Setting {
@ManyToOne
@JoinColumn(nullable = false)
private Configuration configuration;
//more fields and methods
}
模型为主,但多个模型可以使用相同的配置。模型中配置的级联是必需的,因为如果我在配置中更改任何内容,我希望它应用到所有使用此配置的模型中
现在,当我使用具有设置的配置检索现有模型并保存此模型时,不对设置应用任何更改,我得到以下异常
@Transactional
public void doSomething() {
Model model = modelRepository.findOne(0);
//change something in the model, but no changes are made in its configuration
//or do nothing
modelRepository.save(model);
}
我得到以下异常
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Configuration.settings
我怀疑这与延迟加载的设置和休眠试图将空列表合并到配置中有关。
我做错了什么?
检查您在取消引用时尝试作为孤立对象清理的对象的 getter 和 setter:
尝试并使用:
public void setChildren(Set<Child> aSet) {
//this.child= aSet; //Results in this issue
//change to
this.child.clear();
if (aSet != null) {
this.child.addAll(aSet);
} }
问题是由使用 hibernate-enhance-maven-plugin 中的 enableLazyInitialization 引起的。我仍然不知道为什么会导致此错误,但删除此插件解决了问题。
我使用了这个插件,因为我想在模型中延迟加载一个大的字符串字段,我将在应用程序中缓存它。我现在将其更改为延迟获取的 OneToOne 关系。