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.xmlCode:
<?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.xmlCode:
<?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