-->
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.  [ 2 posts ] 
Author Message
 Post subject: discriminator based reference deletes non-orphans on cascade
PostPosted: Tue Sep 06, 2005 12:30 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Mapping documents:
Entity.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Entity" table="ENTITY">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <set name="stringAttributes" table="ATTRIBUTERELATIONSHIP"
    lazy="false" cascade="all-delete-orphan"
    where="DISCRIMINATOR = 1" fetch="subselect">
      <key column="ENTITYID" not-null="true"></key>
      <one-to-many class="StringAttribute" />
    </set>
    <set name="integerAttributes" table="ATTRIBUTERELATIONSHIP"
    lazy="false" cascade="all-delete-orphan"
    where="DISCRIMINATOR = 2" fetch="subselect">
      <key column="ENTITYID" not-null="true"></key>
      <one-to-many class="IntegerAttribute"
      lazy="false"></many-to-many>
    </set>
  </class>
</hibernate-mapping>


Attribute.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Attribute"
  table="ATTRIBUTERELATIONSHIP" discriminator-value="-1">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <discriminator column="DISCRIMINATOR" not-null="true"
    force="true" />
    <subclass name="StringAttribute"
    discriminator-value="1">
      <many-to-one name="stringValue"
      class="StringValue"
      column="VALUEID" not-null="true" cascade="all-delete-orphan"
      lazy="false"></many-to-one>
    </subclass>
    <subclass name="IntegerAttribute"
    discriminator-value="2">
      <many-to-one name="integerValue"
      class="IntegerValue"
      column="VALUEID" not-null="true" cascade="all-delete-orphan"
      lazy="false"></many-to-one>
    </subclass>
  </class>
</hibernate-mapping>


IntegerValue.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="IntegerValue"
  table="INTEGERATTRIBUTE">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <property name="value" column="VALUE"></property>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
session.delete(entity);
session.flush();

Full stack trace of any exception that occurs:
No Exceptions

Name and version of the database you are using:
HSQLDB 1.8.0

The generated SQL (show_sql=true):
Hibernate: delete from ATTRIBUTERELATIONSHIP where ID=?
Hibernate: delete from STRINGATTRIBUTE where ID=?
Hibernate: delete from ATTRIBUTERELATIONSHIP where ID=?
Hibernate: delete from STRINGATTRIBUTE where ID=?
Hibernate: delete from ATTRIBUTERELATIONSHIP where ID=?
Hibernate: delete from INTEGERATTRIBUTE where ID=?
Hibernate: delete from ENTITY where ID=?

Debug level Hibernate log excerpt:
I can paste this in if i really need to

I've removed some properties from these mapping files for brievity. Hopefully all the pertinent info is here.

I am trying to handle cascades with a discriminator without much luck, so i am hoping the hibernate gurus on this forum can help. I have a table ATTRIBUTERELATIONSHIP that looks like the following:
Code:
+------------------+------------+------+-----+---------+-------+
| Field            | Type       | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+-------+
| ID               | bigint(20) |      | PRI | 0       |       |
| VALUEID          | bigint(20) |      | MUL | 0       |       |
| ENTITYID         | bigint(20) |      | MUL | 0       |       |
| DISCRIMINATOR    | tinyint(4) | YES  |     | 1       |       |
+------------------+------------+------+-----+---------+-------+


ENTITYID is a simple FK to an ENTITY table, while VALUEID is a reference to the pk of one of 2 tables STRINGATTRIBUTE and INTEGERATTRIBUTE (determined by the DISCRIMINATOR column). The problem i am seeing is that cascade all-delete-orphan is deleting values from the INTEGERATTRIBUTE table, even though there is a still a reference via ATTRIBUTERELATIONSHIP to that table in conjunction with the DISCRIMINATOR. Is there a clean way to handle this with cascades and mappings, or do i need to cascade save-update and have db maintenance to clean up orphans on a schedule?

Thanks,
Jason


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 12:42 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
I need to include the whole ddl for that ATTRIBUTERELATIONSHIP table, since it's not a simple 2 table join table.
Code:
+------------------+------------+------+-----+---------+-------+
| Field            | Type       | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+-------+
| ID               | bigint(20) |      | PRI | 0       |       |
| VALUEID          | bigint(20) |      | MUL | 0       |       |
| ENTITYID         | bigint(20) |      | MUL | 0       |       |
| ATTRIBUTECLASSID | bigint(20) |      |     | 0       |       |
| DISCRIMINATOR    | tinyint(4) | YES  |     | 1       |       |
+------------------+------------+------+-----+---------+-------+


As you can see there's also a FK to an ATTRIBUTECLASS table here as well, which is why we require the ID, instead of using ENTITYID,VALUEID as the composite PK.


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