-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Many-to-many between inherited classes doesn't work right
PostPosted: Mon Jun 02, 2008 10:06 pm 
Newbie

Joined: Wed Oct 04, 2006 8:11 pm
Posts: 6
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?


Top
 Profile  
 
 Post subject: Solved using a view
PostPosted: Mon Jun 02, 2008 10:33 pm 
Newbie

Joined: Wed Oct 04, 2006 8:11 pm
Posts: 6
I solved this problem by creating a view that shows only the user / outstation relationships of the link table.

But this sounds like a bug to me. I shouldn't have to create a view to get this association to work properly. Hibernate should detect that it is a subclass and should use the discriminator in the query.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.