Hello,
While doing a refactoring where we changed the inheritence strategy we discovered that the generated SQL does not return the same results as before (unit test).
The mapping:
Code:
<joined-subclass abstract="true" name="SRP" table="BK9_T_SYS_RESILIENCE_PLAN">
<key column="PLAN_ID" />
<joined-subclass name="BCASRP" table="BK9_T_SYS_RESILIENCE_PLAN_BCA" >
<key column="PLAN_ID" />
<property column="IS_EXTERNAL_SYSTEM" name="externalSystem" type="boolean" />
</joined-subclass>
<joined-subclass name="INVSRP" table="BK9_T_SYS_RESILIENCE_PLAN_INV">
<key column="PLAN_ID" />
<property column="IS_EXTERNAL_SYSTEM" name="externalSystem" type="boolean" />
</joined-subclass>
</joined-subclass>
The criteria:
Code:
criteria = HibernateUtilImpl.instance().getSession().createCriteria(SRP.class);
if (query.getExternalSystem() != null) {
criteria.add(Restrictions.eq("externalSystem", query.getExternalSystem()));
}
The generated SQL:
Code:
select * FROM bk9_t_sys_resilience_plan this_ INNER JOIN bk9_t_plan this_1_
ON this_.plan_id = this_1_.plan_id
LEFT OUTER JOIN bk9_t_sys_resilience_plan_bca this_2_
ON this_.plan_id = this_2_.plan_id
LEFT OUTER JOIN bk9_t_sys_resilience_plan_inv this_3_
ON this_.plan_id = this_3_.plan_id
LEFT OUTER JOIN bk9_t_system_dependency systemdepe2_
ON this_.plan_id = systemdepe2_.srp_plan_id
WHERE this_2_.is_external_system = 1;
The SQL we whould expect:
Code:
select * FROM bk9_t_sys_resilience_plan this_ INNER JOIN bk9_t_plan this_1_
ON this_.plan_id = this_1_.plan_id
LEFT OUTER JOIN bk9_t_sys_resilience_plan_bca this_2_
ON this_.plan_id = this_2_.plan_id
LEFT OUTER JOIN bk9_t_sys_resilience_plan_inv this_3_
ON this_.plan_id = this_3_.plan_id
LEFT OUTER JOIN bk9_t_system_dependency systemdepe2_
ON this_.plan_id = systemdepe2_.srp_plan_id
WHERE this_2_.is_external_system = 1 or this_3_.is_external_system = 1;
At first while using Hibernate 3.2.6, the two subclasses have been joined with a
INNER JOIN which resulted in results returned from the first joined subclass only. But with Hibernate 3.3.1, the subclasses are now joined with an
LEFT OUTER JOIN which is correct. Now the where clause is wrong. For some reason, the generated SQL contains the where-clause only from one subclass instead of both. Since we are querying for the superclass and not for a superclass, we expect that the SQL should contain both subclasses in the where clause.
We can resolve the issue by moving the common attributes to the superclass, but this is no option since this will break the business domain.
I am thinking about filing a bug against this issue - or do I miss something?
Regards,
Cyrill