-->
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.  [ 4 posts ] 
Author Message
 Post subject: problem by deleting of child in parent list
PostPosted: Sat Mar 24, 2007 2:35 pm 
Newbie

Joined: Sat Mar 24, 2007 12:13 pm
Posts: 2
Hi,

I am newbie in hibernate and want to ask something for many-to-many relation. I have to solve following situation: movie can have a lot of copies, each copy has always one movie. Between customers and copies, there is a separated table called Rental. Rental is quite complicated beacause I have to get copy_id and customer_id as part of the key. The rest of the key is pickupdate.

What I want to do is to delete some copy from the list in parent node movie. When I do this, I get always following exception:

Full stack trace of any exception that occurs:

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [domain.Rental#CopyId: 3, CustomerId: 1, Pickup-date: Fri Jul 20 19:24:02 CEST 2007]
at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1014)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:165)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at util.TransactionManager.commitTransactionInThread(TransactionManager.java:46)
at main.RentalTest.deleteCopies(RentalTest.java:271)
at main.RentalTest.main(RentalTest.java:175)
util.VideoLibraryException: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [domain.Rental#CopyId: 3, CustomerId: 1, Pickup-date: Fri Jul 20 19:24:02 CEST 2007]
at util.TransactionManager.commitTransactionInThread(TransactionManager.java:57)
at main.RentalTest.deleteCopies(RentalTest.java:271)
at main.RentalTest.main(RentalTest.java:175)
Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [domain.Rental#CopyId: 3, CustomerId: 1, Pickup-date: Fri Jul 20 19:24:02 CEST 2007]
at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1014)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:165)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at util.TransactionManager.commitTransactionInThread(TransactionManager.java:46)
... 2 more
util.VideoLibraryException: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [domain.Rental#CopyId: 3, CustomerId: 1, Pickup-date: Fri Jul 20 19:24:02 CEST 2007]
at util.TransactionManager.commitTransactionInThread(TransactionManager.java:57)
at main.RentalTest.deleteCopies(RentalTest.java:271)
at main.RentalTest.main(RentalTest.java:175)
Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [domain.Rental#CopyId: 3, CustomerId: 1, Pickup-date: Fri Jul 20 19:24:02 CEST 2007]
at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1014)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:165)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at util.TransactionManager.commitTransactionInThread(TransactionManager.java:46)
... 2 more



Hibernate version: 3.2.2 AG

I use JPA-annotations and these look like that:

Mapping documents:

In movie I have list of copies with all-delete-orphan:

@OneToMany(mappedBy="movie")
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@LazyCollection(LazyCollectionOption.TRUE)
private Set<Copy> copies = new HashSet<Copy>();

In class customer i have list of rentals:
@OneToMany(mappedBy="customer")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE})
@LazyCollection(LazyCollectionOption.TRUE)
private Set<Rental> rentals = new HashSet<Rental>();

In class copy too:
@OneToMany(mappedBy="copy")
@Cascade({org.hibernate.annotations.CascadeType.DELETE,
org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@LazyCollection(LazyCollectionOption.TRUE)
private Set<Rental> rentals = new HashSet<Rental>();

In rental I use embedded key like that:

@EmbeddedId
private RentalPK id = null;

@ManyToOne(targetEntity=domain.Copy.class)
@JoinColumn(name="COPY_ID", referencedColumnName="COPY_ID", insertable=false, updatable=false)
private Copy copy = null;

@ManyToOne(targetEntity=domain.Customer.class)
@JoinColumn(name="CUSTOMER_ID", referencedColumnName="CUSTOMER_ID", insertable=false, updatable=false)
private Customer customer = null;

and in RentalPK I have the ids of customer and copy:

@Column(name="COPY_FK")
private Integer copyId = null;

@Column(name="CUSTOMER_FK")
private Integer customerId = null;

@Column(name="PICKUP_DATE")
private Date pickUpDate = null;



What I want to do is quite simple
Code between sessionFactory.openSession() and session.close():

DAOFactory factory = DAOFactory.instance(DAOFactory.HIBERNATE);
MovieDAO daoMovie = factory.getMovieDAO(threadId);
Movie movie = daoMovie.getMovieByTitle("Babel");
movie.getCopies().clear();



Name and version of the database you are using: HSQLDB 1.8.0.7

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Could anyone tell what I make wrong in the code? I can do update without problems but by deleting I have always this exception? I can't delete the stuff from db too...

Regards,
music


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:52 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
First, it would be great if you used the code tags to make you more readable. In fact, putting everything in bold is quite uneasy to read...

For your error, I didn't check everything your provided, but I guess you didn't update both directions of the relationship: when deleting a child in the parent list, you must code to assure that:
* the parent has no more reference to the child (in the collection, that's to say)
* the child has a null reference to its parent.

Did you do both?

To do this, it's recommended to have a method in the parent that is responsible for maintaining this integrity, something like:
Code:
public void removeChild(Child c)
{
  children.remove(c);
  c.setParent(null);
}

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 8:55 am 
Newbie

Joined: Sat Mar 24, 2007 12:13 pm
Posts: 2
First thank You for Your answer. I know that I should do something like that, but I supposed that the hibernate does all these things for me. As You can see in my code, I use "all-delete-orphan" at the movie side and will that the hibernate delete all copies too (children from the movie!).

In my case does the hibernate that for me, but I have a problem with association with rental class. Each time when I want to delete the copy from the list in movie, hibernate try to make this node transient too. But there I reach this exception when I want to delete all Rentals because these show onto copy. I am in circle, i suppose.

I don't know ... At least I will do this handy in code, but that is not the real stuff with hibernate.

Thx,

music

batmat wrote:
First, it would be great if you used the code tags to make you more readable. In fact, putting everything in bold is quite uneasy to read...

For your error, I didn't check everything your provided, but I guess you didn't update both directions of the relationship: when deleting a child in the parent list, you must code to assure that:
* the parent has no more reference to the child (in the collection, that's to say)
* the child has a null reference to its parent.

Did you do both?

To do this, it's recommended to have a method in the parent that is responsible for maintaining this integrity, something like:
Code:
public void removeChild(Child c)
{
  children.remove(c);
  c.setParent(null);
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 11:21 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
music wrote:
I don't know ... At least I will do this handy in code, but that is not the real stuff with hibernate.

I don't understand what you say. Be aware that the advice I gave you about maintaining references integrity is not mine.

It's the one you see everywhere, and particularly in the Java Persistence with Hibernate book, so it is the way it should be done, not some view of mine :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.