简体   繁体   English

如何将它们加入到Hibernate HQL查询或Criteria API调用中?

[英]How would I join these in a Hibernate HQL query or Criteria API call?

Person.java: Person.java:

public class Person  implements java.io.Serializable {
    private int id;
    private String fullName;

    public Person() {
    }

    public Person(int id, String fullName) {
        this.id = id;
        this.fullName = fullName;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

ProjectGroup.java: ProjectGroup.java:

public class ProjectGroup  implements java.io.Serializable {
    private int id;
    private Set<Person> persons = new HashSet<Person>(0);
    private String name;

    /**
     * Get the value of name
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
    }

    public ProjectGroup() {
    }


    public ProjectGroup(int id, String name, Set<Person> persons) {
       this.id = id;
       this.name = name;
       this.persons = persons;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public Set<Person> getPersons() {
        return this.persons;
    }

    public void setPersons(Set<Person> persons) {
        this.persons = persons;
    }

}

Person.hbm.xml: Person.hbm.xml:

<hibernate-mapping>
    <class name="com.mycompany.Person" table="person" schema="public">
        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>

        <property name="fullName" type="string">
            <column name="full_name" length="128" not-null="true" />
        </property>

        <set name="projectGroups" inverse="false" lazy="false" cascade="delete" table="group_person">
            <key>
                <column name="project_id" />
            </key>
            <many-to-many entity-name="com.mycompany.ProjectGroup">
                <column name="group_id" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

ProjectGroup.hbm.xml: ProjectGroup.hbm.xml:

<hibernate-mapping>
    <class name="com.mycompany.ProjectGroup" table="project_group" schema="public">
        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="32" not-null="true" />
        </property>
        <set name="persons" inverse="false" table="group_person">
            <key>
                <column name="group_id" />
            </key>
            <many-to-many entity-name="com.mycompany.Person">
                <column name="project_id" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

What I need to do is load all users with a particular group. 我需要做的是用特定的组加载所有用户。 I know how to do that with SQL, but the Hibernate equivalent eludes me. 我知道如何使用SQL做到这一点,但是与Hibernate相当的东西使我难以理解。

First of all, add to your Person class, property projectGroups declared in your Person.hbn.xml . 首先,将在Person.hbn.xml声明的属性projectGroups添加到Person类。 Afterwards, use this criteria to select all users of certain group: 然后,使用此条件选择特定组的所有用户:

Criteria c = session.createCriteria(Person.class);
c.createAlias("projectGroups", "pg");
c.add(Restrictions.eq("pg.name", "project-name"));

Or this HQL: 或此HQL:

from Person as person 
    join person.projectGroups as pg 
        with pg.name = 'project-name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM