-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to keep Parent when deleting child in many-to-one
PostPosted: Wed Oct 12, 2005 4:15 pm 
Newbie

Joined: Mon Feb 07, 2005 8:48 pm
Posts: 6
Im having problem deleting objects. I think there is a problem with my understanding of mapping object in this situation. The idea is such : Movie has an attribute Author ->

So I have this code there :

/**
*
* @hibernate.many-to-one column="author" cascade="none" not-null="false"
* update="true" insert="true"
*/
public Contact getAuthor() {
return author;
}


public void setAuthor(Contact author) {
this.author= author;
}


So I was oirignally thinking that this relationship should be fine - since I have many movies - by 1 author.

But when I try to delete author all movies get deleted. I want them just to have null in the field.

Any help ?

Hibernate version:
3.0


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
[java] [WARN] JDBCExceptionReporter - -SQL Error: -8, SQLState: 23000
[java] [ERROR] JDBCExceptionReporter - -Integrity constraint violation: FK87E69B43B7F9D008 table: MOVIES in statement [d
elete from CONTACTS where ID=1]
[java] [ERROR] AbstractFlushingEventListener - -Could not synchronize database state with session <org.hibernate.excepti
on.ConstraintViolationException: could not delete: [com.scea.csn.data.Contact#1]>org.hibernate.exception.ConstraintViolationE
xception: could not delete: [com.scea.csn.data.Contact#1]
[java] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2086)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2213)
[java] at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:59)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java
:274)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
[java] at com.scea.csn.persistence.DefaultPersistenceManager.remove(DefaultPersistenceManager.java:250)
[java] at com.scea.csn.storage.DefaultStorage.removeItem(DefaultStorage.java:62)
[java] at com.scea.csn.actions.SaveContact.execute(SaveContact.java:45)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:283)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:166)
[java] at com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor.intercept(DefaultWorkflowInterceptor.java:55
)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
[java] at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
[java] at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:116)
[java] at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:272)
[java] at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:237)
[java] at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
[java] at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
[java] at com.opensymphony.webwork.lifecycle.RequestLifecycleFilter.doFilter(RequestLifecycleFilter.java:67)
[java] at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
[java] at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
[java] at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
[java] at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
[java] at com.caucho.server.http.Invocation.service(Invocation.java:315)
[java] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:246)
[java] at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:164)
[java] at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
[java] at java.lang.Thread.run(Thread.java:534)
[java] Caused by: java.sql.SQLException: Integrity constraint violation: FK87E69B43B7F9D008 table: MOVIES in statement [
delete from CONTACTS where ID=1]
[java] at org.hsqldb.Trace.getError(Unknown Source)
[java] at org.hsqldb.jdbcResultSet.<init>(Unknown Source)
[java] at org.hsqldb.jdbcConnection.executeStandalone(Unknown Source)
[java] at org.hsqldb.jdbcConnection.execute(Unknown Source)
[java] at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
[java] at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source)
[java] at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2069)
[java] ... 48 more

[java] [ERROR] DefaultPersistenceManager - -exception removing com.scea.csn.data.Contact@178b64b org.hibernate.exception
.ConstraintViolationException: could not delete: [com.scea.csn.data.Contact#1]
[java] [WARN] JDBCExceptionReporter - -SQL Error: -8, SQLState: 23000
[java] [ERROR] JDBCExceptionReporter - -Integrity constraint violation: FK87E69B43B7F9D008 table: MOVIES in statement [d
elete from CONTACTS where ID=1]
[java] [ERROR] AbstractFlushingEventListener - -Could not synchronize database state with session <org.hibernate.excepti
on.ConstraintViolationException: could not delete: [com.scea.csn.data.Contact#1]>org.hibernate.exception.ConstraintViolationE
xception: could not delete: [com.scea.csn.data.Contact#1]
[java] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2086)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2213)
[java] at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:59)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java
:274)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
[java] at com.scea.csn.persistence.DefaultSessionFactory.endSession(DefaultSessionFactory.java:113)
[java] at com.scea.csn.persistence.DefaultPersistenceManager.endSession(DefaultPersistenceManager.java:74)
[java] at com.scea.csn.persistence.DefaultPersistenceManager.dispose(DefaultPersistenceManager.java:85)
[java] at com.opensymphony.xwork.interceptor.component.DefaultComponentManager.dispose(DefaultComponentManager.java:
92)
[java] at com.opensymphony.webwork.lifecycle.RequestLifecycleFilter.doFilter(RequestLifecycleFilter.java:73)
[java] at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
[java] at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
[java] at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
[java] at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
[java] at com.caucho.server.http.Invocation.service(Invocation.java:315)
[java] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:246)
[java] at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:164)
[java] at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
[java] at java.lang.Thread.run(Thread.java:534)
[java] Caused by: java.sql.SQLException: Integrity constraint violation: FK87E69B43B7F9D008 table: MOVIES in statement [
delete from CONTACTS where ID=1]
[java] at org.hsqldb.Trace.getError(Unknown Source)
[java] at org.hsqldb.jdbcResultSet.<init>(Unknown Source)
[java] at org.hsqldb.jdbcConnection.executeStandalone(Unknown Source)
[java] at org.hsqldb.jdbcConnection.execute(Unknown Source)
[java] at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
[java] at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source)
[java] at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
[java] at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2069)
[java] ... 22 more

[java] [ERROR] DefaultPersistenceManager - -Exception closing session: org.hibernate.exception.ConstraintViolationExcept
ion: could not delete: [com.scea.csn.data.Contact#1]
Name and version of the database you are using:
hsql


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 11:35 pm 
Newbie

Joined: Wed Oct 12, 2005 9:20 pm
Posts: 16
Location: Sydney
I'm also new to Hibernate, but I would say your problem comes from the definition of the Author in Movie :
Quote:
/**
*
* @hibernate.many-to-one column="author" cascade="none" not-null="false"
* update="true" insert="true"
*/
public Contact getAuthor() {
return author;
}


not-null parameter should be set to true. Because the Author can be null.

I hope that's help.


Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 11:47 pm 
Newbie

Joined: Fri Apr 01, 2005 5:21 am
Posts: 11
not-null should be false.

You have to first update the movies individually setting the author to false and then delete the contact.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 6:30 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
vibha131 wrote:
not-null should be false.

You have to first update the movies individually setting the author to false and then delete the contact.


What does you're Author mapping look like ? That's where the cascade action is taking place.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:06 pm 
Newbie

Joined: Mon Feb 07, 2005 8:48 pm
Posts: 6
My Author mapping looks like this :

It has a method getMovies

/**
* @hibernate.bag table="MOVIES" lazy="true" inverse="true"
* @hibernate.collection-one-to-many class="com.scea.csn.data.MovieEffect" cascade="delete"
* @hibernate.collection-key column="author"
*/
public List getMovies() {
return movies;
}

public void setMovies(List movies) {
this.movies = movies;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:14 pm 
Newbie

Joined: Mon Feb 07, 2005 8:48 pm
Posts: 6
So I have to get the movies of the soon-to-be-deleted author and one by one reset author ? then delete author ? seems bulky ? im looking for more a of a reference relationship if such exists where movie - points to author or null..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:36 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
eneymark wrote:
So I have to get the movies of the soon-to-be-deleted author and one by one reset author ? then delete author ? seems bulky ? im looking for more a of a reference relationship if such exists where movie - points to author or null..


Right but the problem is, you've assigned an author to the movie. If you delete the Author and don't update the Movie, the movie will still point to an Author that doesn't exist.

The reason the movies are getting deleted has to do with your cascade= options on the Movie collection of the Author mapping. I actually didn't think cascade="delete" was valid for a one-to-many. I thought this needed to be cascade="delete-orphan" in which case it would delete movies that no longer had a parent Author. You should probably change this to cascade="none" in the Author mapping.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 3:19 pm 
Newbie

Joined: Mon Feb 07, 2005 8:48 pm
Posts: 6
cool :) this solves it. I just hoped there's a way hibernate will magically - update movies - so they just point to null instead of Author that doesnt exist anymore..but guess cascade can't do that.

so I will just update my logic to update every movie and thats it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 3:33 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
eneymark wrote:
cool :) this solves it. I just hoped there's a way hibernate will magically - update movies - so they just point to null instead of Author that doesnt exist anymore..but guess cascade can't do that.

so I will just update my logic to update every movie and thats it.


No, it can't do that. But there is a way to tell your application not to throw an exception if the associated object in a many-to-one doesn't exist. (Not that I'd advocate using this).

There is a property in 3.x for the many-to-one mapping not-found="ignore" which handles this. I'm sure it exists for legacy databases with bad data but it's worth knowing about.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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