删除实体在hibernate中导致ObjectDeletedException [英] deleting entity causes ObjectDeletedException in hibernate
问题描述
1.Item - 是一个制造商和分销商。
2.制造商 - 是由他创建的一组物品。
3.分销商---有一套他分配的项目。
需要关系:
1.当项目被保存/更新时,其制造商和分销商应该被保存/更新
2.当一个制造商被删除时,他的所有物品都应该被删除。
3.当经销商被删除时,他的所有商品都应该被删除。
我试过这样,
class Item {
private Long item_id;
...
私人制造商;
私人经销商经销商;
...
}
Item.hbm.xml
...
<! -
当项目保存时,关联的分销商也是保存
- >
< many-to-one name =distributorclass =Distributorcolumn =DISTRIBUTOR_IDlazy =falsecascade =save-update/>
<! -
当保存项目时,关联的制作器也保存
- >
< many-to-one name =makerclass =Makercolumn =MAKER_IDlazy =falsecascade =save-update/>
...
class Maker {
private Long maker_id;
...
设置< Item>项目;
public Maker(){
items = new HashSet< Item>();
}
...
}
Maker.hbm.xml
...
<! -
当一个Maker所有的物品都被保存下来。
当一个Maker被删除,他的所有项目被删除。
- >
< set name =itemsinverse =truetable =ITEMlazy =falsecascade =all,delete-orphan>
< key column =MAKER_ID/>
< one-to-many class =Item/>
< / set>
...
class经销商{
private Long distributor_id;
...
设置< Item>项目;
public Distributor(){
items = new HashSet< Item>();
}
...
}
Distributor.hbm.xml
<! - 当保存分发服务器时,所有的项目都被保存。
当分销商被删除时,他的所有商品都被删除
- >
< set name =itemsinverse =truetable =ITEMlazy =falsecascade =all,delete-orphan>
< key column =DISTRIBUTOR_ID/>
< one-to-many class =Item/>
< / set>
...
然后我创建了一些实例,并尝试找出是否删除了一个Maker删除所有的项目..
但是,当我尝试这个,我得到这个错误
hibernate.ObjectDeletedException:deleted对象将被级联重新保存(从关联中删除已删除的对象):[myapp.domain.Item#27]
$ b $我认为,这是因为Set项目属于制造商和分销商。我不知道如何正确地建模/映射。
有人可以帮助我吗?我真的采取了我的第一课与hibernate。
真诚
Jim。
main(..){
createEntities();
deleteSomeEntities();
}
public void createEntities(){
session = HibernateUtil.getCurrentSession();
事务tx = null;
制造商maker1 = new Maker();
maker1.setName(maker1);
经销商dis1 = new Distributor();
dis1.setName(dis1);
项目item1 = new Item();
item1.setName(item1);
item1.setMaker(maker1);
item1.setDistributor(dis1);
项目item2 = new Item();
item2.setName(item2);
item2.setMaker(maker1);
item2.setDistributor(dis1);
设置< Item> items = new HashSet< Item>();
items.add(item1);
items.add(item2);
maker1.setItems(items);
dis1.setItems(items);
try {
itemdao.saveOrUpdate(item1);
itemdao.saveOrUpdate(item2);
} catch(RuntimeException e){
logger.error(roll back+ e.getMessage());
tx.rollback();
throw e;
}
}
public void deleteSomeEntities(){
session = HibernateUtil.getCurrentSession();
事务tx = null;
try {
制造商= makerdao.findMakerByName(maker1);
String name = maker.getName();
logger.info(got maker:+ name);
makerdao.deleteMaker(maker);
tx.commit();
} catch(RuntimeException e){
logger.info(rolling back);
tx.rollback();
throw e;
}
}
您会收到此错误,因为您尝试删除一个对象,但该对象在某些父对象列表中被引用,并且您的传递持久性(即级联)设置被设置为使得父级控件的关系。换句话说,你给出了hibernate冲突的命令:你告诉它删除对象,但是你也告诉它,如果对象在指定的集合中,请执行一个保存。
只需从父项中的集合中删除您要删除的对象,或更改级联/反向映射。
I am trying to map cascading relations between 3 entities in hibernate.The entities are
1.Item --has a Maker and a Distributor.
2.Maker --has a set of items created by him.
3.Distributor ---has a set of items he distributes.
Relations needed:
1.When an Item is saved/updated ,its Maker and Distributor should be saved/updated
2.When a Maker is deleted ,all his items should be deleted.
3.When a Distributor is deleted,all his items should be deleted.
I tried like this,
class Item{
private Long item_id;
...
private Maker maker;
private Distributor distributor;
...
}
Item.hbm.xml
...
<!--
when Item is saved the associated Distributor is also saved
-->
<many-to-one name="distributor" class="Distributor" column="DISTRIBUTOR_ID" lazy="false" cascade="save-update"/>
<!--
when Item is saved the associated Maker is also saved
-->
<many-to-one name="maker" class="Maker" column="MAKER_ID" lazy="false" cascade="save-update"/>
...
class Maker{
private Long maker_id;
...
Set<Item> items;
public Maker(){
items = new HashSet<Item>();
}
...
}
Maker.hbm.xml
...
<!--
when a Maker is saved,all his items are saved.
when a Maker is deleted,all his items are deleted.
-->
<set name="items" inverse="true" table="ITEM" lazy="false" cascade="all,delete-orphan">
<key column="MAKER_ID" />
<one-to-many class="Item" />
</set>
...
class Distributor{
private Long distributor_id;
...
Set<Item> items;
public Distributor(){
items = new HashSet<Item>();
}
...
}
Distributor.hbm.xml
<!--when a Distributor is saved, all his items are saved.
when a Distributor is deleted, all his items are deleted
-->
<set name="items" inverse="true" table="ITEM" lazy="false" cascade="all,delete-orphan">
<key column="DISTRIBUTOR_ID" />
<one-to-many class="Item" />
</set>
...
Then I created some instances and tried to find out if deleting a Maker deletes all his items.. However,when I try this,I get this error
hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [myapp.domain.Item#27]
I think ,it is because the Set items belongs to both Maker and Distributor.I am not sure how to model/map this properly.. Can someone help me out?I am really taking my first lessons with hibernate.
sincerely
Jim.
main(..){
createEntities();
deleteSomeEntities();
}
public void createEntities(){
session = HibernateUtil.getCurrentSession();
Transaction tx = null;
Maker maker1 = new Maker();
maker1.setName("maker1");
Distributor dis1 = new Distributor();
dis1.setName("dis1");
Item item1 = new Item();
item1.setName("item1");
item1.setMaker(maker1);
item1.setDistributor(dis1);
Item item2 = new Item();
item2.setName("item2");
item2.setMaker(maker1);
item2.setDistributor(dis1);
Set<Item> items = new HashSet<Item>();
items.add(item1);
items.add(item2);
maker1.setItems(items);
dis1.setItems(items);
try{
itemdao.saveOrUpdate(item1);
itemdao.saveOrUpdate(item2);
}catch(RuntimeException e){
logger.error("rolling back"+e.getMessage());
tx.rollback();
throw e;
}
}
public void deleteSomeEntities(){
session = HibernateUtil.getCurrentSession();
Transaction tx = null;
try{
Maker maker = makerdao.findMakerByName("maker1");
String name = maker.getName();
logger.info("got maker:"+name);
makerdao.deleteMaker(maker);
tx.commit();
}catch(RuntimeException e){
logger.info("rolling back");
tx.rollback();
throw e;
}
}
whenever you get this error, its because you are trying to delete an object, but that object is referenced in some parent objects list, and your transitive-persistence (i.e. cascade) settings are set such that the parent controls the relationship. In other words, you are giving hibernate conflicting commands: you are telling it to delete the object, but you are also telling it that if the object is in the specified collection, do a save.
Just remove the object you are trying to delete from the collection in the parent, or change your cascade/inverse mappings.
这篇关于删除实体在hibernate中导致ObjectDeletedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!