Hibernate version: 3.1
Name and version of the database you are using:Oracle 10G
I'm having problems mapping our legacy database schema. The problem is that it's actually created in a too generic way. I'll try to sketch the situation with a stripped down example:
Code:
TABLE OUR_OBJECTS
columns:
NUMBER ID
VARCHAR TYPE
VARCHAR TITLE
TABLE OBJECT_TYPE1
columns:
NUMBER OBJ_ID (foreign key to OUR_OBJECTS.ID)
NUMBER EXTRA_INFO
TABLE OBJECT_TYPE2
columns:
NUMBER OBJ_ID (foreign key to OUR_OBJECTS.ID)
VARCHAR SUBTITLE
TABLE OBJECT_LINKS
columns:
NUMBER MASTER_OBJ_ID (foreign key to OUR_OBJECTS.ID)
NUMBER DETAIL_OBJ_ID (foreign key to OUR_OBJECTS.ID)
We have 3 types:
-type1 (join with OBJECT_TYPE1 table)
-type2 (join with OBJECT_TYPE2 table)
-type3 (no joins needed)
Relations:
type1 is master of exactly one type2
type1 is master of multiple type3's
Basically this follows the Table per subclass, using a discriminator architecture rather closely.
Mapping documents:Code:
<class name="ParlObject" table="OUR_OBJECTS" discriminator-value="">
<id name="id" column="ID" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">OBJ_SEQ</param>
</generator>
</id>
<discriminator type="java.lang.String" column="TYPE" force="true"/>
<property name="title" column="TITLE" type="java.lang.String" />
<subclass name="Type1" discriminator-value="TYPE1">
<set name="type3Set" table="OBJECT_LINKS">
<key column="MASTER_OBJ_ID"/>
<many-to-many class="Type3" column="DETAIL_OBJ_ID" where="TYPE = 'TYPE3'" />
</set>
<join table="OBJECT_LINKS">
<key column="MASTER_OBJ_ID"/>
<many-to-one name="type2" column="DETAIL_OBJ_ID" />
</join>
<join table="OBJECT_LINKS">
<key column="MASTER_OBJ_ID"/>
<many-to-one name="type4" column="DETAIL_OBJ_ID"/>
</join>
<join table="OBJECT_TYPE1">
<key column="OBJ_ID"/>
<property name="extraInfo" column="EXTRA_INFO">
</join>
</subclass>
<subclass name="Type2" discriminator-value="TYPE2">
<join table="OBJECT_TYPE2">
<key column="OBJ_ID"/>
<property name="subTitle" column="SUBTITLE">
</join>
</subclass>
<subclass name="Type3" discriminator-value="TYPE3">
</subclass>
</class>
The problem I have now is when I load an entity of Type1, I get an exception:
org.hibernate.HibernateException: More than one row with the given identifier was found
This is because when he writes the underlying query he gets multiple possibilities for it's type2's (namely all other type3's)
I fixed the lookup allready a bit by adding a where clause for the type3Set lookup, but there is no where-clause available on a many-to-one...
I also tried specifying to use a fetch="select" on the join for the type2 as well as on the many-to-one of type2, but to no success. He still tries to load the entity as a whole.
Is there a way to better map this schema?
Or is there any chance a where could be added on the many-to-one ? (for any hibernate developers out there)
TIA
PS: I tried replacing the join with a formula for the many-to-one of type2, but performance took a big hit when writing hql with joins because of all hte resulting sub-queries