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: Deleting from unidirectional many-to-many
PostPosted: Sun Aug 07, 2005 7:54 am 
Newbie

Joined: Thu Apr 28, 2005 3:12 pm
Posts: 7
Hibernate version: 3:0
Name and version of the database you are using:PostgreSql 8


Hi.

I have class Person with a Set friends which is unidirectional many-to-many
to Person.

class Person {
...
/**
* @hibernate.set
* table="FRIENDS_OF_PERSONS"
* lazy="true"
* cascade="none"
* @hibernate.collection-key
* column="HAVE_A_FRIEND"
* @hibernate.collection-many-to-many
* class="com.polatach.model.Person"
* column="IS_A_FRIEND"
*/
public Set getFriends() {
return friends;
}
...
}

Hibernate makes new tabl, FRIENDS_OF_PERSONS

create table FRIENDS_OF_PERSONS (
HAVE_A_FRIEND int8 not null,
IS_A_FRIEND int8 not null,
primary key (HAVE_A_FRIEND, IS_A_FRIEND)
)
alter table FRIENDS_OF_PERSONS add constraint FK6E3EBDA854972751 foreign key (HAVE_A_FRIEND) references PERSONS

alter table FRIENDS_OF_PERSONS add constraint FK6E3EBDA836CBE45C foreign key (IS_A_FRIEND) references PERSONS



I have a problem with session.delete(person).
When I want to delete a Person who have friends in Set friends (in table FRIENDS_OF_PERSONS he appears in HAVE_A_FRIEND column) there is no problem:
Hibernate: delete from FRIENDS_OF_PERSONB where HAVE_A_FRIEND=?
Hibernate: delete from PERSONS where PERSONS_ID=?

But when i want to delete a person who is a friend(in table FRIENDS_OF_PERSONS he appears in IS_A_FRIEND column, there is an error from database:

13:50:44,500? WARN JDBCExceptionReporter:57 - SQL Error: 0, SQLState: null

13:50:44,500? ERROR JDBCExceptionReporter:58 - Batch entry 0 delete from PERSONS where PERSONS_ID=691 was aborted. Call getNextException to see the cause.

13:50:44,500? WARN JDBCExceptionReporter:57 - SQL Error: 0, SQLState: 23503

13:50:44,500? ERROR JDBCExceptionReporter:58 - ERROR: update or delete on "persons" violates foreign key constraint "fk6e3ebda836cbe45c" on "FRIENDS_OF_PERSONS"


So my question is how to delete person object wich is a friend. And why hibernate do not generate sql:

alter table FRIENDS_OF_PERSONS add constraint FK6E3EBDA854972751 foreign key (HAVE_A_FRIEND) references PERSONS ON DELETE CASCADE


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 8:49 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
This is limitation of current implementation of Hibernate - it's working on object model level, and can't automagicaly remove any references from everywhere. You should either remove such references manualy in code, or use biderectional associations with on-delete="cascade" attribute on "key" element.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 3:20 pm 
Newbie

Joined: Mon Oct 17, 2005 2:55 pm
Posts: 1
sergeya wrote:
biderectional associations with on-delete="cascade" attribute on "key" element.


what about many-to-many associations, on-delete="cascade" doesn't seem to be allowed. i have the exact same type of associations and i can't seem to find a combination of cascades that will remove the row in the assocation table when a person is removed, but not the group as a whole.


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.