Joined: Mon Apr 04, 2005 4:14 pm Posts: 1
|
Hibernate version: 3.0
Name and version of the database: Oracle 8i
I have 2 classes A (with 2 sub-classes A1, A2) and B. There is a many-to-many relationship between these two classes represented by C
Mapping of A:
------------
<hibernate-mapping package="eg.tp">
<class name="A" table="A" discriminator-value="a">
<id name="uniqueId" type="string" length="20" column="UNIQUEID">
<generator class="assigned"/>
</id>
<discriminator column="TYPE" type="string" not-null="true" length="20" force="true" />
...
<set name="setB" inverse="true" cascade="all-delete-orphan" table="C">
<key column="UNIQUEID"/>
<many-to-many class="B" column="UNIQUEID"/>
</set>
<subclass name="A1" discriminator-value="a1" lazy="true" />
<subclass name="A2" discriminator-value="a2" lazy="true" />
</class>
</hibernate-mapping>
Mapping of B:
------------
<hibernate-mapping package="eg.tp">
<class name="B" table="B">
<id name="uniqueId" column="UNIQUEID" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">B_SEQ</param>
<param name="schema">EG</param>
</generator>
</id>
...
<set name="setA1" table="C">
<key column="UNIQUEID" />
<many-to-many class="A1" column="UNIQUEID" />
</set>
<set name="setA2" table="C">
<key column="UNIQUEID" />
<many-to-many class="A2" column="UNIQUEID" />
</set>
</class>
</hibernate-mapping>
HQL Query - Get all records of subclass A2:
------------------------------------------
(Class S has a one-to-many relationship with A called SetB)
select distinct s.col1
from S as s
join s.setB as sa
join sa.setA1 as sd
where sd.id=:id
and sd.class=A1
HQL Query - Get all records of subclass A2:
------------------------------------------
select distinct s.col1
from S as s
join s.setB as sa
join sa.setA2 as sd
where sd.id=:id
and sd.class=A2
Code fragment:
-------------
b is an instance of B
for (Iterator it = b.getSetA1().iterator(); it.hasNext();) {
A1 sd = (A1) it.next();
}
for (Iterator it = b.getSetA2().iterator(); it.hasNext();){
A2 sd2 = (A2) it.next();
}
The above fragment for iterating A2 gives me an error:
Object with id: 234 was not of the specified subclass
This is because it does not use the dicriminator to get specific sub-classes
From some of the posts I read that setting a 'where' property in the 'set' tag would solve this problem
I changed the set tag to:
<set name="setA1" table="C" where="TYPE='a1'">
<key column="UNIQUEID" />
<many-to-many class="A1" column="UNIQUEID" />
</set>
<set name="setA2" table="C" where="TYPE='a1'">
<key column="UNIQUEID" />
<many-to-many class="A2" column="UNIQUEID" />
</set>
This however appended the where clause to my HQL queries as well causing them to fail. It added
the clause c.TYPE='a1' (instead of a.type='a1') and c.TYPE='a2' (instead of a.type='a2').
Since my HQL queries occur before the code fragments I couldn't check to see if the code fragments executed correctly.
Is there something I can do to fix this?
Any help would be appreciated.
|
|