-->
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.  [ 11 posts ] 
Author Message
 Post subject: Cascade set null
PostPosted: Wed Apr 21, 2010 4:55 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I have a relation: A one-to-many B
If A is deleted the foreign key in B needs to be set to null

Can I use cascade for this?
If so how?

I used cascade="save-update", but now when A is deleted the foreign key in B remains so this will throw:
Quote:
No row with the given identifier exists


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 6:38 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name="PARENT_ID",nullable=true)

nullable=true is what you are looking for. hope this helps

Please do not forget to rate.


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 7:09 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Note: I use hbm.xml files.

not-null="false" is the default and I already used save-update.
So that doesn't work.


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 7:50 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
have u mapped unidirectional or bidirectional.


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 9:00 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
bidirectional


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 9:31 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
can you post your mapping files.


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Wed Apr 21, 2010 10:02 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Code:
<class name="data.Position" table="positions">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <set name="storeEmployees" table="storeEmployees" inverse="true" cascade="save-update">
         <key column="position" />
         <one-to-many class="data.StoreEmployee" />
      </set>
   </class>
Code:
<class name="data.StoreEmployee" table="storeEmployees">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <many-to-one name="position" class="data.Position" column="position" lazy="false" />
   </class>


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Thu Apr 22, 2010 4:29 am 
Newbie

Joined: Thu Apr 22, 2010 3:31 am
Posts: 1
you can have a try to set the inverse="false", then you can delete the Position object ,the column position is set null.
aukemid wrote:
Code:
<class name="data.Position" table="positions">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <set name="storeEmployees" table="storeEmployees" inverse="true" cascade="save-update">
         <key column="position" />
         <one-to-many class="data.StoreEmployee" />
      </set>
   </class>
Code:
<class name="data.StoreEmployee" table="storeEmployees">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <many-to-one name="position" class="data.Position" column="position" lazy="false" />
   </class>


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Thu Apr 22, 2010 4:35 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Thanks, that solved it. I can't believe I didn't try that.

Ps. Can someone remove that spammer: zstarsales04


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Thu Apr 22, 2010 5:49 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
what did you try.


Top
 Profile  
 
 Post subject: Re: Cascade set null
PostPosted: Thu Apr 22, 2010 5:50 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
sorry ignore me


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