Hibernate cascade =“all-delete-orphan”,不会删除孤儿 [英] Hibernate cascade="all-delete-orphan", doesn't delete orphans
问题描述
我在使用Hibernate删除孤立节点时遇到了以下映射问题:
@OneToMany(fetch = FetchType.LAZY,mappedBy =seizure,orphanRemoval = true)
@JsonManagedReference
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
public Set< SubstanceIdentified> getSubstanceIdentifieds(){
return this.substanceIdentifieds;
}
.hbm.xml映射就像这样
< set name =substanceIdentifiedstable =substance_identifiedinverse =truelazy =truefetch =selectcascade =all-删除-孤儿>
< key>
< column name =seizure_idnot-null =true/>
< / key>
< / set>
我使用Spring MVC和Jackson来执行JSON到Hibernate的类映射。 b
@RequestMapping(value = {/save.json},method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj,模型模型){
seizureService.saveOrUpdate(seizureObj);
注意:
seizureObj
在其<
seizureObj
的id属性设置为数据库中的现有记录。但是,当我调用saveOrUpdate现有记录(孤儿)不会被删除。
< seizure服务使用Spring的
getHibernateTemplate.saveOrUpdate
我已经阅读了有关
的主题 JPA CascadeType.ALL不会删除孤儿
Hibernate在更新集合时删除孤儿
看来我的设置是正确的。
我必须
- 首先从数据库中加载相应对象,并首先使用我的反序列化对象)
- 删除对其他对象的引用
- 保存更改
- 更新我的反序列化对象
?
Acutally可以在没有调用clear的情况下执行。
我只需要调用右键函数。
$ b
使用 HibernateDaoSupport.getHibernateTemplate()。merge(object)
在我的代码中,我首先测试jackson
的反序列化对象是否附加了一个ID
如果是这样,我调用save,if不是我调用合并。
if(obj.getId()){
myDAO.save(obj);
} else {
myDAO.merge(obj);
}
并且我的DAO的合并函数是这样定义的。
public void merge(E transientObject){
getHibernateTemplate()。merge(transientObject);
}
这会删除它应该是的孤儿。
如果有人遇到同样的问题,请不要犹豫,我愿意帮助您。
问候JS
I am having trouble deleting orphan nodes using Hibernate with the following mapping
@OneToMany(fetch = FetchType.LAZY, mappedBy = "seizure",orphanRemoval=true)
@JsonManagedReference
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
public Set<SubstanceIdentified> getSubstanceIdentifieds() {
return this.substanceIdentifieds;
}
the .hbm.xml mapping is like this
<set name="substanceIdentifieds" table="substance_identified" inverse="true" lazy="true" fetch="select" cascade="all-delete-orphan">
<key>
<column name="seizure_id" not-null="true" />
</key>
<one-to-many class="org.unodc.incbszdb.db.base.SubstanceIdentified" />
</set>
I use Spring MVC and Jackson to do the JSON to Hibernate Class mapping
@RequestMapping(value = { "/save.json" }, method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj, Model model) {
seizureService.saveOrUpdate(seizureObj);
NOTE:
seizureObj
has only two NEW entries in its substanceIdentifieds
Set.
The id property of seizureObj
is set to an existing record in the db. But when I call saveOrUpdate existing records (orphans) don't get deleted.
Seizure Service uses Spring's
getHibernateTemplate.saveOrUpdate
I have read the threads about
JPA CascadeType.ALL does not delete orphans
Hibernate deleting orphans when updating collection
It seems my setup is right.
Do I have to
- load the according object from the DB in a dummy object object first (with the ID my de-serialized object has)
- delete the references to other object
- save the changes
- update with my de-serialized object
?
Acutally it is possible to do it without calling clear.
All I had to do is calling the right function.
use HibernateDaoSupport.getHibernateTemplate().merge(object)
In my code I first test if the de-serialized object from jackson has a ID attached
if so I call save, if not I call merge.
if(obj.getId()){
myDAO.save(obj);
}else{
myDAO.merge(obj);
}
and the merge function of my DAO is defined like this.
public void merge(E transientObject) {
getHibernateTemplate().merge(transientObject);
}
This deletes the orphans like it is supposed to be.
If somebody is facing the same problem do not hesitate, I am open to help you with it.
Regards JS
这篇关于Hibernate cascade =“all-delete-orphan”,不会删除孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!