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: A and B delete challenge
PostPosted: Thu Oct 02, 2008 1:26 pm 
Beginner
Beginner

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

I have two classes with many-to-many
A with aid as primary key
B with bid as primary key
The join table(A_B) is not explicitly defined(it does not have its own mapping file). And has the foreign keys as the only two columns coming from their respective tables.
Task at hand is to delete a relationship between a specific A and a specific B(not the A or B objects themselves).
So in non-hibernate way I could do that by

delete from A_B where aid=? and bid=?;

How do I accomplish that in Hibernate?

I have tried A.getList().remove(sess.load(B.class,someid))
In this case it throws me an error ::
a different object with the same identifier value was already associated with the session:

The reason is that I have lazy turned false due to some reasons and when I say load within the delete it tries to load that object but its already there in the memory.
So hhow do I get around this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 2:56 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
The error is thrown because here

Quote:
I have tried A.getList().remove(sess.load(B.class,someid))


you are trying to load that child object twice into the session. An object with "someId" is already present in the session.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 3:40 pm 
Beginner
Beginner

Joined: Fri May 11, 2007 11:03 am
Posts: 32
Hi gonzao
I know that and I mentioned that already in original post. The question I have is that how go around this situation. How to delete the relationship between two objects(with many to many) where you need to set lazy="false" ?
The above conditions are given and I need to find solution based on those conditions.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 2:31 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Create an A.removeChild(String childId) method.
Inside that method, first remove the object from A's inner children collection.
Then issue an HQL query in the line of "delete from Child where childId=?"

_________________
Gonzalo Díaz


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.