-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Exception when deleting the parent
PostPosted: Sat Feb 24, 2007 11:13 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Hi,

I have a many-to-one relationship between LocationProfile and Address. Suppose I have Address1 referenced by LocationProfile1. I can successfully delete Address1, however if I try to search for all LocationProfiles right after that I receive the following exception:

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.mycompany.domain.model.Address#68]

You can see full stack trace at the end of this message.

The second line in the following code throws the exception:
Code:
addressDao.save(address);
persistedLocProfs = locationProfileDao.findAll(); // throws ObjectDeletedException


Here is how I define Address in LocationProfile.hbm.xml:
Code:
<many-to-one name="address" class="Address" cascade="save-update"/>


Here is how I define LocationProfile collection in Address.hbm.xml:
Code:
<set name="locationProfiles" cascade="delete" inverse="true">
         <key column="address"/>
         <one-to-many class="LocationProfile"/>
      </set>


It seems that when I search for LocationProfiles (children) Hibernate tries to re-save deleted Address (parent) because child cascades save-update on the parent.

Any help will be greatly appreciated.

Thanks.

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.mycompany.domain.model.Address#68]
Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.mycompany.domain.model.Address#68]
at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1007)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:168)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:509)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:501)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:962)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1107)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at
...
com.mycompany.domain.dao.hibernate.GenericDaoImpl.findAll(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 25, 2007 2:31 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
I fixed the problem by doing Session.refresh(address) before deleting address.

Here is the sequence of steps I am taking now (all steps run within the same transaction):
1. Create transient address.
2. Create transient location profiles and associate them with the address.
3. saveOrUpdate(address)
4. refresh(address)
5. delete(address)
6. createQuery("from " + LocationProfile.class.getName()).list()

What I don't understand is why I need to refresh address (step 4) to prevent step 6 throwing "deleted object would be re-saved by cascade" exception (see my first post)?

Thanks.[/list]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.