-->
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.  [ 1 post ] 
Author Message
 Post subject: HQL query: joining two many-to-many collections
PostPosted: Tue Sep 30, 2008 3:42 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Hi!

I need some help with a HQL query.
First my entity model:
I have the entity User which has a set of TemplateGroups, the entity Template which also has a set of TemplateGroups and the entity TemplateGroup which has sets of Users and Templates (so they are mapped many-to-many).

Now the query:
I want to get all Templates where any of the TemplateGroups of a Template exist in the TemplateGroups of a given User. So if User and Template have at least one identical TemplateGroup, the Template should be selected.

I have no problems creating a SQL query for my needs where I join the mapping tables between User/Template and TemplateGroup, but since I have mapped the collections as many-to-many (the mapping tables don't have any additional columns) I can't find a HQL query for my needs.

Could someone please help my with that query?


Hibernate version:
2.0.1.GA

Mapping documents:
User.hbm.xml:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.Library" namespace="Project.Library.BOs">
    <class name="User" table="[User]">
        <id name="ID">
            <generator class="identity"/>
        </id>
[snip]
        <set name="TemplateGroups" table="User_TemplateGroup">
            <key column="UserID"/>
            <many-to-many class="TemplateGroup" column="TemplateGroupID"/>
        </set>
    </class>
</hibernate-mapping>

Template.hbm.xml:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.Library" namespace="Project.Library.BOs">
    <class name="Template">
        <id name="ID">
            <generator class="identity"/>
        </id>
[snip]
        <set name="TemplateGroups" table="Template_TemplateGroup">
            <key column="TemplateID"/>
            <many-to-many class="TemplateGroup" column="TemplateGroupID"/>
        </set>
    </class>
</hibernate-mapping>

TempateGroup.hbm.xml:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.Library" namespace="Project.Library.BOs">
    <class name="TemplateGroup">
        <id name="ID">
            <generator class="identity"/>
        </id>
[snip]
        <set name="Users" table="User_TemplateGroup" inverse="true">
            <key column="TemplateGroupID"/>
            <many-to-many class="User" column="UserID"/>
        </set>
        <set name="Templates" table="Template_TemplateGroup" inverse="true">
            <key column="TemplateGroupID"/>
            <many-to-many class="Template" column="TemplateID"/>
        </set>
    </class>
</hibernate-mapping>


Name and version of the database you are using:
Microsoft SQL Server 2005


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

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.