-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-2-Many, cascade="none" problem
PostPosted: Tue Jan 11, 2005 11:10 am 
Newbie

Joined: Tue Jan 11, 2005 10:49 am
Posts: 3
Hibernate version: 2.1.3 & 2.1.6

Mapping documents:
I use hibernate with spring in Tomcate, and I use the org.springframework.orm.hibernate.support.HibernateDaoSupport class for my DAO.

I have a relation Person <--1----*-->Book and in the Person class I have

private java.util.Collection book;
/**
* @hibernate.set
* lazy="true"
* cascade="none"
* @hibernate.collection-key
* column="OWNER_FK"
* @hibernate.collection-one-to-many
* class="com.mebi.entity.Book"
*/
public java.util.Collection getBook(){
return book;
}
public void setBook(java.util.Collection book){
this.book = book;
}

and in my Book class I have

private com.mebi.entity.Person owner;
/**
* @hibernate.many-to-one
* column="OWNER_FK"
* not-null="false"
* outer-join="true"
* unique="false"
* cascade="none"
*/
public com.mebi.entity.Person getOwner(){
return owner;
}
public void setOwner(com.mebi.entity.Person owner){
this.owner = owner;
}

but when I assign a Book to a Person and then I want to delete that person (the delete is done in a separate action and transaction), the person entity will be deleted and the book entity's foreign key will be set to null (the save-update cascade action!!!). Please pay attention that the cascade for Person to Book is set to none.

The code for delete is
getHibernateTemplate().delete("from Person obj where obj.id = '"+id+"'");[/b]


Top
 Profile  
 
 Post subject: Re: One-2-Many, cascade="none" problem
PostPosted: Tue Jan 11, 2005 12:35 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
mebi wrote:
...
but when I assign a Book to a Person and then I want to delete that person (the delete is done in a separate action and transaction), the person entity will be deleted and the book entity's foreign key will be set to null ...


And what, may I ask, did you expect to happen?


Top
 Profile  
 
 Post subject: Re: One-2-Many, cascade="none" problem
PostPosted: Wed Jan 12, 2005 4:05 am 
Newbie

Joined: Tue Jan 11, 2005 10:49 am
Posts: 3
drj wrote:
mebi wrote:
...
but when I assign a Book to a Person and then I want to delete that person (the delete is done in a separate action and transaction), the person entity will be deleted and the book entity's foreign key will be set to null ...


And what, may I ask, did you expect to happen?


As you see, the cascade on the parent side is none, so when I delete a Person(parent side here), Hibernate shouldn't delete the one side, and then it must alert me.
But, when I don't mention the one side(my relation is unidirectional with <many-to-one/> tag only, which has a cascade=none) when I want to delete the one side I'll get a HibernateException for constaint failure(the foreign key constraint violation).

I want to say that in any case when I set the cascade=none, the delete action of parent must cause an exception to be thrown, because when I set cascade=none, Hibernate supposed to don't react in a save-update manner, but here it reacts in save-update manner.

I want to have a bidirectional one-2-many association, and when I want to delete the parent (which has at least one child), the hibernate throws an exception about foreign key constraint violation. I don't want hibernate delete the parent side and update all the children's foreign key to null. So what should I do?


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