I believe I have found a bug when using projections via the criteria api in a table per subclass hierarchy.
I essentially have 4 different tables. Table A is the parent of Tables B, C, and D, e.g. table B, C, and D have a reference to the PK of Table A via a FK.
I have classes ClassA, ClassB, ClassC, and ClassD that map to the corresponding tables and B,C,D extend A.
So here is where the bug comes in. I actually have a field, let's call it "someField", that is common to both Class B and C, but not D. Therefore I can't put it in ClassA.
So in my ClassA.hbm.xml file I have mapped Class B, C, and D via <joined-subclass> tags and in the definitions of Class B and C I have a property defined called "someField".
<joined-subclass name="com.acme.ClassB"
<property name="someField" type="java.lang.String">
<column name="
SOME_FIELD_B" length="35" />
</property>
</joined-subclass>
<joined-subclass name="com.acme.ClassC"
<property name="someField" type="java.lang.String">
<column name="
SOME_FIELD_C" length="45" />
</property>
</joined-subclass>
That being said I should now be able to setup a criteria query using Projections to just select "someField" from both table B and C. e.g. :
Code:
getSession().createCriteria(ClassA.class)
.setProjection(Projections.projectionList()
.add(Property.forName("someField"))
.list();
The resulting sql is incorrectly generated as :
select
this_1_.SOME_FIELD_C as y0_,
from TABLE_A this_,
TABLE_B this_1_,
TABLE_C this_2_,
where this_.SRC_REC_ID=this_1_.SRC_REC_ID(+)
and this_.SRC_REC_ID=this_2_.SRC_REC_ID(+)
This is wrong because TABLE_B does not contain SOME_FIELD_C!!! it contains SOME_FIELD_B!!
Now I would suspect that you are now saying "Why don't you just create a new class, ClassBC that contains the common field, someField". This may indeed be the way to go, but there should at least be an error message at startup that indicates that you can't map a field with the same name in a hierarchy.
My question to the hibernate developers is : "Why couldn't you have two fields with the same name in 2 different classes in a hierarchy?". The generated sql could just map to both mapped properties in the appropriate classes.
I'd like to get some feedback on this to see what others think before I post it to JIRA.
Thanks,
Dave
FYI : I'm using
Hibernate version: 3.2.3 GA[/code]