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