-->
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: issue with defining collection mapping
PostPosted: Wed Nov 04, 2009 1:00 pm 
Newbie

Joined: Wed Nov 04, 2009 11:50 am
Posts: 3
Hi All,

Could any one please read this and suggest how I should address my problem.

Brief explanation of my problem:
When the given java code snippet executes , permissions collection is invoked from hbm.xml. It generates SQL query that contains join between PERMISSION and PERSISTENCE which is not like what I expect. PERMISSION table has a column PERMISSIBLE_ID which is a foreign-key to PERSISTENCE table. So the hibernate SQL query generated should contain a join temp1_.id=temp1_1_.PERMISSIBLE_ID. However I may try I was never able to accomplish that by changing the hbm.xml files. I have been struck on this for 2 days.


The main difference between the SQL queries below is the join between PERMISSION and PERSISTENCE table when the permissions collection is used. It should be as high lighted. Please ignore the rest of the joins.


Expected Result:
But the expected query should be of the form:

select this_.id as y0_ from PERSISTENCE this_ inner join PERSISTENCE_CONTEXT this_1_ on this_.id=this_1_.ID left outer join PERMISSION temp1_1_ on temp1_.id=temp1_1_.PERMISSIBLE_ID where this_.CLASS_NAME='81' and temp1_1_.PARTICIPANT_ID in (?, ?, ?, ?)

Actual Result:
This is generating an actual query of the form (I have removed unnecessary things in the generated SQL query):

select this_.id as y0_ from PERSISTENCE this_ inner join PERSISTENCE_CONTEXT this_1_ on this_.id=this_1_.ID left outer join PERMISSION temp1_1_ on temp1_.id=temp1_1_.ID where this_.CLASS_NAME='81' and temp1_1_.PARTICIPANT_ID in (?, ?, ?, ?)

which is giving temp1_.id=temp1_1_.ID instead of temp1_.id=temp1_1_.PERMISSIBLE_ID

My questions:
1) could any one give inputs how can I define the set permissions in a way that gives my desired query because the set only seems to be the right way to tell hibernate about which property it should use while preparing joins. Please correct me if I am wrong.
2) or could any one give some inputs to make the hibernate generated SQL query contain the join temp1_.id=temp1_1_.PERMISSIBLE_ID



Code Snippet:
Code:
Projection projection = Property.forName("id");
    Criteria cr = persistSession.getSession()
   .createCriteria(WorkAreaContext.class)
   .createAlias("permissions","temp"); // Create an alias for the collection object permissions
   
    cr.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    Collection test = new HashSet();
    test.add(161); test.add(163); test.add(167); test.add(171);
    cr.add(Restrictions.in("temp.participantId", test));
    cr.setProjection(projection);
    retVector = cr.list();




Hibernate mappings:

Code:
<hibernate-mapping package="model" default-lazy="false">

    <class name="Persistence" table="PERSISTENCE">
        <id name="id" unsaved-value="0">
            <generator class="native" />
        </id>
        <discriminator column="CLASS_NAME" insert="false" />
        <version name="version" column="VERSION" type="long" />
        <property name="classType" column="CLASS_NAME" type="byte" />
    </class>

    <subclass name="Permissible" extends="Persistence" discriminator-value="95">

        <property name="defaultPermission" column="DEFAULT_PERM" type="integer" />
        <property name="hasPermissions" column="HASPERMISSIONS" type="boolean" />

        <set name="permissions"  table="PERMISSION" fetch="subselect">
            <key column="ID" foreign-key="PERMISSIBLE_ID"/>
            <!-- Tried <key column="PERMISSIBLE_ID"/> but that didnt work at all -->
            <one-to-many class="model.Permission" />
        </set>

    </subclass>

    <subclass name="PersistenceContext" extends="Permissible">
        <join table="PERSISTENCE_CONTEXT">
            <key column="ID" />
             <!-- Some more properties are skipped for the sake of convenience -->
            <property name="name" column="NAME" type="string" />
        </join>
        <subclass name="ObjectContext" extends="PersistenceContext" discriminator-value="31">

            <subclass name="WorkAreaContext" discriminator-value="81">
                <join table="PERSISTENCE_CONTEXT">
                    <key column="ID" />
                    <!-- Some more properties are skipped for the sake of convenience -->
                </join>
            </subclass>
   
        </subclass>

    </subclass>

    <subclass name="Permission" extends="Persistence" discriminator-value="97">

        <join table="PERMISSION" >
            <key column="ID" />
            <property name="participantId" column="PARTICIPANT_ID" type="long" />
            <property name="permissibleId" column="PERMISSIBLE_ID" type="long" />
             <!-- Some more properties are skipped for the sake of convenience -->
        </join>
    </subclass>
</hibernate-mapping>


Thank you much in advance,
Ramchander.


Top
 Profile  
 
 Post subject: Re: issue with defining collection mapping
PostPosted: Thu Dec 03, 2009 2:22 am 
Newbie

Joined: Wed Nov 04, 2009 11:50 am
Posts: 3
Hi All,

I have used many-to-many relation instead of one-to-many relationship and hence the query is generated as I expect.

Ramchander.


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.