I started using <union-subclass>, mainly for polymoriphic associations.
I spotted some performance issue with selecting top level abstract objects by 'where' clause.
Imagine AbstractAB and two concrete subclasses ConcreteA and ConcreteB. While getting AbstractAB
by id hibernate issues something like:
select a.* (select * from ConcreteA union select * from ConcreteB) as a where a.id = ?.
And here is the problem. Why 'where'clause is not issued for each union selects but for whole combined result? it's not really efficient.
I tried to work that around and write my <loader query. I did have a problems though with sql and aliases.
Let's have simplified:
<class name="AbstractAB" abstract="true">
<property name="id"/>
<property name="description"/>
<loader query-ref="loadAbstractAB"/>
</class>
<union-subclass name="ConcreteA" extends="AbstractAB">
</union-subclass>
<union-subclass name="ConcreteB" extends="AbstractAB">
</union-subclass>
Here is the problem. Currently it looks like that:
<sql-query name="loadAbstractAB">
<return class="ConcreteA" alias="a"/>
<return class="ConcreteB" alias="b"/>
SELECT {a.*} FROM ConcreteA a WHERE a.id=:id
UNION
SELECT {b.*} FROM ConcreteB b WHERE b.id=:id
</sql>
When I load the AbstractAB I got:
IntegerType:132 - could not read column value from result set: id4_1_; Column 'id4_1_' not found.
Strange thing is that if i copy/paste generated SQL it executes correctly in database !
Does anyone have any clue? And what about that union-subclass 'where' clause, why is applied
to the combined results not partial?
|