Hi,
I've found an interesting behaviour, maybe it's normal and I'm a stupid user. So the model is the following.
There is an abstract SuperObject class, which has two subclasses, SubObject_Single.class and SubObject_Compound.class.
There is another class, CommonObject, which has a component object, CalendarComponent, which has a Calendar object.
SubObject_Single has a one-to-one relationship to CommonObject.
SubObject_Compound has two component objects (Component.class), which have one-to-one relationship to CommonObject.
My problem is, when I want to load a SubObject_Single object with
Code:
session.load(SubObject_Single.class, 1)
, I get the following exception.
Code:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [hibernate.problem.SuperObject#1]
I've checked there is an object with id 1 in the db.
On the other side loading a SubObject_Compound object, it works fine.
I've checked the sql and the problem is, that Hibernate joins the SubObject_Compound's component's CommonObject (huhhh!) properties with inner join.
Here is the mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.problem">
<class name="SuperObject" lazy="false">
<id name="id" type="integer" column="id" />
<property name="name" type="string" />
<joined-subclass name="SubObject_Single" lazy="false">
<key column="id"></key>
<many-to-one name="common" class="CommonObject"
cascade="all" unique="true" not-null="true"/>
</joined-subclass>
<joined-subclass name="SubObject_Compound" lazy="false">
<key column="id"></key>
<component name="component1" class="ComponentObject">
<many-to-one name="common" class="CommonObject"
column="common1" cascade="all" unique="true" not-null="true" />
</component>
<component name="component2" class="ComponentObject">
<many-to-one name="common" class="CommonObject"
column="common2" cascade="all" unique="true" not-null="true" />
</component>
</joined-subclass>
</class>
<class name="CommonObject" lazy="false">
<id name="id" type="integer" column="id" />
<property name="name" type="string" />
<component name="calendarComponent">
<many-to-one name="calendar" class="Calendar"
column="calendar_id" not-null="true" />
</component>
</class>
<class name="Calendar" lazy="false">
<id name="id" type="integer" column="id" />
<property name="name" type="string" />
</class>
</hibernate-mapping>
and here is the generated sql:
Code:
select superobjec0_.id as id0_4_,
superobjec0_.name as name0_4_,
superobjec0_1_.common as common1_4_,
superobjec0_2_.common1 as common2_2_4_,
superobjec0_2_.common2 as common3_2_4_,
case
when superobjec0_1_.id is not null then 1
when superobjec0_2_.id is not null then 2
when superobjec0_.id is not null then 0
end as clazz_4_,
commonobje1_.id as id3_0_, commonobje1_.name as name3_0_,
commonobje1_.calendar_id as calendar3_3_0_, calendar2_.id as id4_1_,
calendar2_.name as name4_1_, commonobje3_.id as id3_2_,
commonobje3_.name as name3_2_, commonobje3_.calendar_id as calendar3_3_2_,
commonobje4_.id as id3_3_, commonobje4_.name as name3_3_,
commonobje4_.calendar_id as calendar3_3_3_
from SuperObject superobjec0_
left outer join SubObject_Single superobjec0_1_ on superobjec0_.id=superobjec0_1_.id
left outer join SubObject_Compound superobjec0_2_ on superobjec0_.id=superobjec0_2_.id
left outer join CommonObject commonobje1_ on superobjec0_1_.common=commonobje1_.id
left outer join Calendar calendar2_ on commonobje1_.calendar_id=calendar2_.id
inner join CommonObject commonobje3_ on superobjec0_2_.common1=commonobje3_.id
inner join CommonObject commonobje4_ on superobjec0_2_.common2=commonobje4_.id
where superobjec0_.id=?
If I set the fetch attribute to select in the SubObject_Compound.ComponentObject's many-to-one associations, it works fine, so it's a solution. But I'm wondering, whether this is normal behaviour or not?
I have made a test application, if somebody is interested or wants more info I can send it.
I'm using Hibernate 3.2 with MSSQL Server
thx,
Ivan
Thanks for the Hibernate team you've made a great stuff