-->
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: Deleting just the relationship
PostPosted: Thu Sep 11, 2008 12:28 pm 
Beginner
Beginner

Joined: Fri May 11, 2007 11:03 am
Posts: 32
Hibernate version:3.2

Mapping documents:
A.hbm.xml

<class name="A"
table="A">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="child"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="afk"/>
</key>
<many-to-many class="B" not-found="ignore">
<column name="bfk"/>
</many-to-many>
</bag>
</class>


B.hbm.xml

<class name="B"
table="B">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="child"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="bfk"/>
</key>
<many-to-many class="A" not-found="ignore">
<column name="afk"/>
</many-to-many>



Name and version of the database you are using:Oracle 10g

As you see its a manytomany relation.
I want to be able to delete the relation between A and B for a A.
For example I want to remove the relation between A and B for A with id =1
In SQL its easy . Just remove the rows with A id foreign key in the join table , but since I dont have any direct mapping for the join table how would do that. Note: I dont want to remove the B objects , only the relationship.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 3:43 pm 
Newbie

Joined: Thu Oct 26, 2006 11:50 am
Posts: 17
Location: Chesterfield, VA
I'm assumming there is a table which stores Ids for A and B to make the many to many work. Create a class to represent that table, then you can just delete those records.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 4:52 pm 
Beginner
Beginner

Joined: Fri May 11, 2007 11:03 am
Posts: 32
I dont have mapping for the join table .


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 5:13 pm 
Beginner
Beginner

Joined: Fri May 11, 2007 11:03 am
Posts: 32
First I want to correct the mappings:

A.hbm.xml

<class name="A"
table="A">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="a"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="afk"/>
</key>
<many-to-many class="B" not-found="ignore">
<column name="bfk"/>
</many-to-many>
</bag>
</class>


B.hbm.xml

<class name="B"
table="B">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="a"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="bfk"/>
</key>
<many-to-many class="A" not-found="ignore">
<column name="afk"/>
</many-to-many>


Secondly,I managed to get rid of the problem by using
A.getB().removeAll();
sess.flush();

Now I have A without any b objects.
And in the same transaction I want to add some objects of B some of them might/might not be the same as the ones deleted.
So, before I do A.getB().add(b1), where b1 is an object of B type,
I have to load b1 by doing the sess.load(....).
As long as load the objects which were not there in A previously , it works fine, but as soon as I try to load one of the B object which was in A proviously, it throws an error , "a different object with the same identifier was associated in the session" .
Since I have already done sess.flush(). i thought it would have cleared the B objects in A. but thats not the case. Even sess.evict(A) does not seem to work for me.


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.