Hibernate version:
3.2
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.prosrm.airline.groupmask.domain.Principal" table="SY_PRNCPLS">
<id name="id" type="long">
<generator class="increment" />
</id>
<discriminator column="TYPE" type="string" />
<property name="name" />
<subclass name="com.prosrm.airline.groupmask.domain.Role" discriminator-value="ROLE">
</subclass>
<subclass name="com.prosrm.airline.groupmask.domain.Outstation" discriminator-value="WORKTEAM">
<join table="gm_outstation_parameter">
<key column="principal_id"/>
<property name="outstationId" column="outstation_name"/>
<property name="defaultCurrency" column="default_currency"/>
</join>
</subclass>
<subclass name="com.prosrm.airline.groupmask.domain.User" discriminator-value="USER">
<set name="outstations" table="sy_prncpl_relationships">
<key column="parentid" />
<many-to-many column="childid" class="com.prosrm.airline.groupmask.domain.Outstation"/>
</set>
<join table="SY_USER_INFO">
<key column="id"/>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<property name="email"/>
</join>
</subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using:Oracle 10g
The generated SQL (show_sql=true):Code:
select
outstation0_.parentid as parentid1_,
outstation0_.childid as childid1_,
outstation1_.id as id7_0_,
outstation1_.name as name7_0_,
outstation1_1_.outstation_name as outstation2_8_0_,
outstation1_1_.default_currency as default3_8_0_
from
sy_prncpl_relationships outstation0_,
SY_PRNCPLS outstation1_,
gm_outstation_parameter outstation1_1_
where
outstation0_.childid=outstation1_.id(+)
and outstation1_.id=outstation1_1_.principal_id(+)
and outstation0_.parentid=?
Debug level Hibernate log excerpt:
The problem
I have an inheritance structure with three subtypes. Users, Roles, Workteams (also called outstations in our system). Each of these is defined in a SY_PRNCPLS table. Users can have multiple outstations and multiple roles. These associations between user and role and between user and outstation are mapped in the SY_PRNCPL_RELATIONSHIPS table.
The problem I'm having is that a user can have many outstations (mapped as a set), but when I try to load the outstations, it is loading roles as well. Specifically, it is creating Outstation objects out of role records.
It looks like Hibernate does not use the discriminator to make sure that it is pulling back Outstations, even though I specified that they are Outstations in the mapping. It seems to be pulling back the data from the Role record and constructing an Outstation out of it. I'm guessing the query is assuming that a link table only link two things together, but in this case, the table is mapping three things together.
How can I get Hibernate to use the discriminator in this query to make sure its only pulling back Outstation objects and not other subclasses of Principal?