-->
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: How to delete object references?
PostPosted: Tue Jan 30, 2007 5:04 am 
Newbie

Joined: Mon Jan 22, 2007 10:16 am
Posts: 5
Hi!

I've been having problems removing an object that has references to it. I've searched everywhere, but none of the solutions helped me. Maybe I didn't know what exactly to search for at all...

Here's the problem:

- Assume we have objects Foo and Bar.

- Foo contains a List of Bars.

Foo's mapping file is something like this:
Code:
<hibernate-mapping>
   <class name="Foo" table="Foo">

       <id name="id" column="PKEY">
             <generator class="identity"/>
       </id>
   
   <property name="name" column="Name" not-null="true" unique="true"/>
        
   <list name="bars" table="Foo_Bars" lazy="false">
      <key column="FK_Foo"/>
      <list-index column="SortOrder"/>
      <many-to-many column="FK_Bar" class="Bar"/>
   </list>
      
   </class>
</hibernate-mapping>


Bar's mapping file actually doesn't relate to the problem, assume its just a simple class with one string attribute or something.

No, when I try to delete the Bar, I get a REFERENCE contraint error, because Foo has references to Bar.
Yes, I could (manually) delete all the references to Bar in the relation table Foo_Bars, before deleting Bar itself, but I will have an unknown number of different objects pointing to the Bar the same way Foo does.

Is my Foo mapping file just plain wrong, or is this problem a very tough one?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 5:18 pm 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
I think Hibernate dont only remove association.

If you add cascade in your mapping hibernate is try delete association and bars. If you dont add cascade hibernate delete only foo

_________________
Please don't forget to rateME!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 31, 2007 3:41 am 
Newbie

Joined: Mon Jan 22, 2007 10:16 am
Posts: 5
OK, I have solved my own problem.

All I needed to do was this:
Code:
<list name="bars" table="Foo_Bars" cascade="delete-orphan" lazy="false">
      <key column="FK_Foo"/>
      <list-index column="SortOrder"/>
      <one-to-many class="Bar"/>
   </list>


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.