2012-09-04 5 views
0

Person.java :Hibernate HQL 질의 또는 Criteria API 호출에서 이들을 어떻게 합치겠습니까?

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 :

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로 :

<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 :

<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> 

내가 무엇을 필요로하는지 do는 특정 그룹의 모든 사용자를로드합니다. 나는 SQL을 사용하여이를 수행하는 방법을 알고 있지만, Hibernate와 동등한 점은 나를 피할 수있다.

답변

1

우선 Person.hbn.xml에 신고 된 Person 클래스의 속성 projectGroups에 추가하십시오.

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

또는이 HQL :

from Person as person 
    join person.projectGroups as pg 
     with pg.name = 'project-name' 
그 후, 특정 그룹의 모든 사용자를 선택하려면이 기준을 사용