Hibernate version: 3.2.0
The generated SQL (show_sql=true):
Code:
select areasofcou0_.countrycode as countryc3_1_, areasofcou0_.code as code1_,
areasofcou0_.code as code2_0_, areasofcou0_1_.isactive as isactive2_0_,
areasofcou0_1_.countrycode as countryc3_2_0_, areasofcou0_.name as name3_0_
from TLAreas_en areasofcou0_
inner join TLAreas areasofcou0_1_
on areasofcou0_.code=areasofcou0_1_.code
where areasofcou0_.displayforhotels = 1 and areasofcou0_.countrycode=88 order by areasofcou0_.name;
Hi.
I have a problem that me and my mate at work are trying to solve. We have 2 tables at database that are TLAreas and TLAreas_en, joinable by a field 'code'. I need one class to represent them as one joined table. The subselect option with hand-written sql query works but doesn't fulfill all our needs (read only...). So, after RTFMing, I've decided to map as following - a superclass for TLAreas and derived for TLAreas_en, mapped as joined subclass (see included mapping xmls). This worked fine and fulfilled all our needs, until I've decided to define a one-to-many mapping (in other class mapping definition) that shall be consisted of TLAreas_en objects. More specific, I have a table with countries so I've tried to create a set with all the relevant areas for each country. This mapping doesn't works. (sql exceptions about wrong query). When I tried to look on a query that you can see on top of the message, you can see that it starts with
select areasofcou0_.countrycode when actually it shall use
areasofcou0_1_ alias as the field countrycode defined at a table aliased as areasofcou0_1_ and not areasofcou0_ (TLAreas and not TLAreas_en)... The same problem repeats again at the query.
My work mate thinks that this is a bug in hibernate and is going to fix it at a hibernate source level. I don't believe that such a serious issue (as you can't map a set with objects that are joined subclass) may really be not known so far, so, may be, there is a workaround?
Thanks a lot.
Including mapping xmls:
The xml with an area mapping:Code:
<hibernate-mapping>
<class
name=" datamodel.codes.localized.LocalizedArea" table="TLAreas">
<cache usage="read-write"/>
<synchronize table="TLAreas" />
<synchronize table="TLAreas_en" />
<id name="code" type=" java.lang.Integer">
<column name="code" />
<generator class="assigned" />
</id>
<property name="isactive" type=" java.lang.Boolean">
<column name="isactive" length="100" not-null="true" />
</property>
<property name="countrycode" type=" java.lang.Integer">
<column name="countrycode" length="100" not-null="true" />
</property>
<set name="hotelsInArea" lazy="false" cascade="save-update"
inverse="true" order-by="name">
<key column="areacode" />
<one-to-many class="datamodel.codes.localized.LocalizedHotel_en" />
</set>
<joined-subclass name="datamodel.codes.localized.LocalizedArea_en " table="TLAreas_en">
<key column="code"/>
<property name="name" column="name" type="java.lang.String"/>
</joined-subclass>
</class>
</hibernate-mapping>
The sets of areas definition from another class xml mapping (the one that doesn't works): Code:
<set name="areasOfCountry" lazy="false" cascade="save-update" order-by="name"
inverse="true" >
<cache usage="read-write"/>
<key column="countrycode" />
<one-to-many class="datamodel.codes.localized.LocalizedArea_en " />
</set>
<set name="areasOfCountryForHotels" lazy="false" cascade="save-update" order-by="name"
inverse="true" where="displayforhotels = 1">
<cache usage="read-write"/>
<key column="countrycode" />
<one-to-many class="datamodel.codes.localized.LocalizedArea_en" />
</set>