-->
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.  [ 3 posts ] 
Author Message
 Post subject: Difficult HQL Query Question
PostPosted: Fri Jun 25, 2004 5:51 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hi I am trying to write a query and am struggling to get it to do what I want.. I know my association here isn't the greatest, but it has to do for now. Here is my mapping for this object:

Code:
<hibernate-mapping>
    <class name="UserType" table="adminUserType">
      <id name="id" type="long" unsaved-value="null">
           <generator class="identity"/>
        </id>
        <property name="description"/>
        <set name="roles" table="adminUserType_Role" lazy="true">
            <key>
                <column name="userType_id" not-null="true"/>
            </key>
            <many-to-many class="Role">
                <column name="role_id" not-null="true"/>
            </many-to-many>
        </set>
        <property name="name"/>
        <property name="rootPath"/>
        <set name="users" cascade="none" lazy="true" inverse="true">
         <key column="userType_id"/>
         <one-to-many class="User"/>
        </set>
        <set name="userTypesWithPermissionToAssign" table="adminUserType_UserType" lazy="true" sort="UserTypeNameComparator">
            <key>
                <column name="userType_id" not-null="true"/>
            </key>
            <many-to-many class="UserType">
                <column name="userType2_id" not-null="true"/>
            </many-to-many>
        </set>
        <property name="userClassName"/>
    </class>

</hibernate-mapping>


So basically each UserType object contains a circular reference to a set of UserType's it has access to work with. The query I am trying to execute needs to select a subset of userTypesWithPermissionToAssign from a given userType id. Here is what I've tried so far to no avail:

Code:
      Query userTypesQuery =
        session.createQuery(
          "select elements(ut.userTypesWithPermissionToAssign) from UserType ut " +
          "left join fetch ut.userTypesWithPermissionToAssign userType where ut.id=:ID and userType.userClassName=:USERCLASSNAME");
      userTypesQuery.setParameter("ID", loggedInUser.getUserType().getId());
      userTypesQuery.setParameter("USERCLASSNAME", loggedInUser.getUserType().getUserClassName());
      List userTypesList = userTypesQuery.list();


This appears to work except it is returning multiple objects of each of the actual usertypes it should return... I suppose I could just put it into a set and eliminate the repeates but I'd rather just have it return the correct amount. When I try to use left join fetch distinct user.userTypesWithPermissionToAssign i just get errors.

Thanks in advance!
-David


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The distinct keyword won't help you in case of a join fetch. The set is the best solution

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:40 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Ok thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.