建议hql下面的sql

时间:2013-08-03 05:46:27

标签: hibernate hql

下面是sql查询,它将获取结果,这是许多内部联接的组合......相应的pojos是

 Table             JAVA POJO
    rtyuUSER          User
    rtyu_GROUP         rtyu

user.java包含以下属性

updatedby     updatebby     updateddate

rtyu_GROUP.java包含以下属性

ID

code;

名;

获取记录的查询..     SELECT distinct u.name,u.updated_by,u.updateddate FROM rtyuUSER u,rtyuuser_rtyu ug,rtyurtyu gwhere u.id = ug.user_id and ug.rtyu_id = g.id and u.ACTIVE_FLAG =' Y'并且g.id不是(10,11)的顺序由u.name

并且表之间的关系在hbm中定义为

<class name="User" table="rtyu_USER" dynamic-update="true" select-before-update="true">
<property name="name" type="string" />
<property name="updated" type="timestamp" column="UPDATEDDATE"  />
        <property name="updatedBy" column="UPDATED_BY" type="string"/>
        <property name="active" type="yes_no" column="FLAG" />
        <set name="groups" table="rtyu_USER_GROUP" lazy="true" inverse="false" sort="natural">
            <key column="USER_ID" />
            <many-to-many column="GROUP_ID" class="Group" />
        </set>


        and the nother class tag is defined as..

        <class name="Group" table="rtyu_GROUP" mutable="false">
        <id name="id" unsaved-value="0" type="long" access="property" />
        <property name="code" type="string" />
        <property name="name" type="string" />
        <set name="permissions" table="rtyu_GROUP_PERMISSION" inverse="true" lazy="false" sort="natural">
            <key column="GROUP_ID" />
            <many-to-many column="PERMISSION_ID" class="Permission" />
        </set>
    </class>



Now could you please advise how to write HQL for this for this..folks please advise for this

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那将会是:

SELECT DISTINCT FROM
User u
INNER JOIN u.groups as g
WHERE u.active='Y' AND g.id NOT IN (10,11)
ORDER BY u.name