-->
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.  [ 1 post ] 
Author Message
 Post subject: Integrity constr. violation when deleting a referenced obj.
PostPosted: Mon Jun 09, 2008 7:42 am 
Newbie

Joined: Mon Jun 09, 2008 7:17 am
Posts: 2
Hi,

I have the following associations between my objects:

A<>----B
<>----C
C------>B

So A contains B and C.
C references B.

Mapping for class A is

<class name="A" table="A">
...
<set name="b" inverse="true"
cascade="all-delete-orphan">
<key column="a_id" />
<one-to-many class="B" />
</set>

<set name="c" inverse="true"
cascade="all-delete-orphan">
<key column="a_id" />
<one-to-many class="C" />
</set>
</class>

So the lifetime of B and C depends on the lifetime of A.

The reference between B and C is bidirectional and looks as follows

<class name="B" table="B">
...
<many-to-one name="a" column="a_id"
not-null="true" />

<set name="cs" inverse="true">
<key column="b_Id" />
<one-to-many class="C" />
</set>

</class>

<class name="C" table="C">
...
<many-to-one name="a" column="a_id"
not-null="true" />
<many-to-one name="b"
column="b_Id" not-null="false"/>
</class>

I add some object instances to the model and everything works fine.

A a;
B b;
C c;

a.addToBs(b);
a.addToCs(c);
c.setB(b);
b.addToCs(c);

But when I delete an instance of B and a reference from an instance of C to B still exists, I get a

Integrity constraint violation FKD5AC618F4BC6603A table: C in statement [delete from B where Id=?]

Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
a.removeFromBs(b);
s.getTransaction().commit();


But what I expect is that the reference from C to B is automatically deleted (e.g. the foreign key is set to null), when B is deleted.

I already tried to add the cascade=delete to the <set name="cs" inverse="true">, but this resulted in a
"deleted object would be re-saved by cascade (remove deleted object from associations):" exception.

Is there anybody who can help me? Thanks in advance.

I am using Hibernate Version 3.2.6 and HSQLDB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.