-->
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.  [ 6 posts ] 
Author Message
 Post subject: Generating Cascaded Delete on Foreign Key
PostPosted: Thu Oct 06, 2005 6:02 am 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
Hibernate version:
version 3.0.5, 25 May 2005

Mapping documents:
<class name="Queue" table="queue_names" abstract="true" discriminator-value="Q">
<id name="queueID" column="qid" type="long">
<generator class="native"/>
</id>
<discriminator column="type" type="character"/>
<property name="queueName" type="string" length="48"/>

<set name="membership" table="queue_memb">
<key column="qid"/>
<composite-element class="Queue">
<property name="queueID" column="gid" type="long"/>
</composite-element>
</set>

<set name="attributes" table="queue_values" inverse="true">
<key column="qid" on-delete="cascade"/>
<one-to-many class="Attribute"/>
</set>

<subclass name="User" discriminator-value="U">
</subclass>

<subclass name="Group" discriminator-value="G">
</subclass>
</class>
Code between sessionFactory.openSession() and session.close():

User u1 = new User (1, "user1");
User u2 = new User (2, "user2");
Group g1 = new Group (3, "group1");
Group g2 = new Group (4, "group2");
Group g3 = new Group (5, "group3");

// add user1 to group1
u1.addmembership (g1);

// add user1 and group2 to group3
u1.addmembership (g3);
g2.addmembership (g3);

tx = session.beginTransaction ();

session.save (u1);
session.save (u2);
session.save (g1);
session.save (g2);
session.save (g3);

session.flush ();

tx.commit ();

Full stack trace of any exception that occurs:
n/a

Name and version of the database you are using:
MS-SQL 2000 (8.00.760 SP3)

The generated SQL (show_sql=true):
n/a

qid type queueName
--------------------- ---- ------------------------------------------------
1 U user1
2 U user2
3 G group1
4 G group2
5 G group3

(5 row(s) affected)

qid gid
--------------------- ---------------------
1 3
1 5
4 5

Debug level Hibernate log excerpt:
n/a

Short question:

How can I get a delete cascade foreign key on a set that doesn't use one-to-many but uses a composite element? Maybe the way I'm going about storing the data through Hibernate is wrong. I am new to all this :)

Long question:

I have created a single Queue class, each instance of which represents a row in the queue_names table. There are two sub classes of this called User & Group, which are just used to distinguish between different types of queues in the table.

A Queue can contain (only when a Group) a Set of class instances that are what the queue is a member of i.e. if the set contained a "group1" class instance then that queue is a member of "group1" etc...

Membership is stored by a simple mapping table (queue_memb) that stores the queues id and the id of the queue that it is a member of. This means that there can be none or multiple rows in this table for a single row in the queue_names table.

Since I only use an element of the Queue class (the ID) when writing rows to the queue_memb table this is what I specify in the hbm file for the Queue class, however this doesn't allow me to create a delete cascaded foreign key, whereas I can for a one-many-mapping for a seperate class.

Is there a better way of representing the need to store only the Queue classes ID value when writing data to the queue_memb table that will get me a cascaded delete foreign key?

Cheers :)


Top
 Profile  
 
 Post subject: Update & Further Observations
PostPosted: Thu Oct 06, 2005 9:03 am 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
Another, possibly unrelated issue, to using this current model is that Hibernate performs some deletes when populating the two tables:

Hibernate: insert into queue_names (queueName, type) values (?, 'U') select scope_identity()
Hibernate: insert into queue_names (queueName, type) values (?, 'U') select scope_identity()
Hibernate: insert into queue_names (queueName, type) values (?, 'G') select scope_identity()
Hibernate: insert into queue_names (queueName, type) values (?, 'G') select scope_identity()
Hibernate: insert into queue_names (queueName, type) values (?, 'G') select scope_identity()
Hibernate: insert into queue_memb (qid, gid) values (?, ?)
Hibernate: insert into queue_memb (qid, gid) values (?, ?)
Hibernate: insert into queue_memb (qid, gid) values (?, ?)
Hibernate: delete from queue_memb where qid=? and gid=?
Hibernate: insert into queue_memb (qid, gid) values (?, ?)
Hibernate: insert into queue_memb (qid, gid) values (?, ?)
Hibernate: delete from queue_memb where qid=? and gid=?
Hibernate: insert into queue_memb (qid, gid) values (?, ?)

Simple question: why? Probably requires a complicated answer :(

Is it the fact that it inserts *all* instances of the main class into both tables and then only deletes those that aren't in the Set? I doubt this due to the number of rows inserted and deleted in the queue_memb table.

Thanks.


Top
 Profile  
 
 Post subject: I have the same problems
PostPosted: Fri Apr 14, 2006 2:36 am 
Newbie

Joined: Fri Apr 14, 2006 1:50 am
Posts: 8
Location: Peking
who can help?

_________________
bravery can set you free.


Top
 Profile  
 
 Post subject: It has been sometime...
PostPosted: Fri Apr 14, 2006 2:56 am 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
I wish I knew :)


Top
 Profile  
 
 Post subject: Re: It has been sometime...
PostPosted: Fri Apr 14, 2006 3:11 am 
Newbie

Joined: Fri Apr 14, 2006 1:50 am
Posts: 8
Location: Peking
homeworkjunkie wrote:
I wish I knew :)


since you posted the problem several month ago, I think you should find a workaround, huh?

_________________
bravery can set you free.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 5:58 am 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
This problem was found during investigations into whether Hibernate could be used in our product. With most things we end up implementing our own code to implement certain functionality e.g. queueing, events, data partioning etc...

Hey ho :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.