-->
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: Problems with Delete Statement
PostPosted: Thu Oct 20, 2005 7:53 am 
Regular
Regular

Joined: Sat Sep 03, 2005 9:07 am
Posts: 87
Location: Graz, AUSTRIA
Hibernate version:3.1

Hi all!

I have a problem with a Delete Statement.

I want to delete a object out of a Many-to-Many Relation (Tables Contact and Table Partner). If I just use session.delete(contact) then all contacts with the ID of this object are deleted (also in the association table -->in this table there is the Primary Key of Table Contact and the primary key of Table Partner)
So If the contact was binded to another Partner, this contact is ALSO deleted!

Here you see the SQL Hibernate makes:
Code:
Hibernate: /* delete collection com.magnasteyr.editool.hibernate.EtContacts.etEdiPartnerMany */ delete from EDITOOLS.ET_PA2CON where CON_ID=?
Hibernate: /* delete com.magnasteyr.editool.hibernate.EtContacts */ delete from EDITOOLS.ET_CONTACTS where CON_ID=?



But I only want to delete contacts out of the association table that have the ID of contact AND the ID of partner

I want something like that:
Code:
"Delete from EtContact c where                c.etEdiPartnerMany.paId=:paId and
c.conId=:conId";


How can I achieve that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:13 am 
Newbie

Joined: Wed Apr 20, 2005 4:14 am
Posts: 10
Location: Netherlands
As I understand it, you'd have to delete a relation and not an object.

So you would have to change your code to something like:
Code:
//Imagine that a cat can be friends with other cats (many-to-many)
Cat c = (Cat) Session.get(id)
c.friends.delete(3); //The 3rd cat is not his friend anymore, but the 3rd cat still exists, and still can be friends with other cats.
Session.update(c);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:22 am 
Regular
Regular

Joined: Sat Sep 03, 2005 9:07 am
Posts: 87
Location: Graz, AUSTRIA
I want to delete an contact out of the contact table (Primary Key ConId)
and i want to delete the entry out of the association table where the primary key of conID is the ID of the contact I want to delete and the primary key of partner is the ID of the partner I'm editing.


When I just use session.delete ALL Entries in the association table are deleted, that have the COnID of the Contact I deleted?

Do you unterstand what I mean?


Here is my er-model:
Image


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:40 am 
Regular
Regular

Joined: Sat Sep 03, 2005 9:07 am
Posts: 87
Location: Graz, AUSTRIA
It works:

like this:

Code:
EtBusinessCon businessCon =(EtBusinessCon)session.get(EtBusinessCon.class,bcId);
         
EtEdiPartner edipartner=(EtEdiPartner)session.get(EtEdiPartner.class,paId);
         
businessCon.getEtEdiPartnerMany().remove(edipartner);
session.update(businessCon);
         
   if(businessCon.getEtEdiPartnerMany().size()==0)
      {
         session.delete(businessCon);
      }
session.flush();


BIG THX!!!!


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.