使用QBC的方式应用多对多关系中的查询

关系描述

         User实体和Group实体是多对多的关系,实体和表的对应关系如下

Java Bean

User
tb_user
Group
tb_group

tb_user表的字为:ID,UserName,FirstName,LastName

tb_group表的字段为:ID,Name

其中关联关系中间表为 :tb_userGroup ,其字段为:ID,UserID(外键,引用tb_user表的id),GroupID(外键,引用tb_group表的id)

User实体的代码如下                             

public class User{

 
      private  Long  id ;
 

      private String userName;

 

      private String firstName;

 

      private String lastName;

 

      private Set<Group> groupList = new HashSet<Group>();

 
      // Constructors
 
      /** default constructor */
      public  User() {
      }
 
      public  Long getId() {

            return this.id;

      }
 

      public void setId(Long id) {

            this.id = id;

      }
 
      public  String getUserName() {

            return this.userName;

      }
 

      public void setUserName(String userName) {

            this.userName = userName;

      }
 
      public  String getFirstName() {

            return this.firstName;

      }
 

      public void setFirstName(String firstName) {

            this.firstName = firstName;

      }
 
      public  String getLastName() {

            return this.lastName;

      }
 

      public void setLastName(String lastName) {

            this.lastName = lastName;

      }
 
      public  String getUsername() {

            return this.userName;

      }
 

      public Set<Group> getGroupList() {

            return  groupList ;
      }
 

      public void setGroupList(Set<Group> groupList) {

            this.groupList = groupList;

      }
 
}
 

Group实体代码如下:


public class Group implements java.io.Serializable {
     private Long id;

     private String name;

     private Set<User> userList = new HashSet<User>();

     // Constructors

     /** default constructor */
     public Group() {
     }

     public String getName() {
          return this.name;
     }

     public Long getId() {
          return id;
     }

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

     public void setName(String name) {
          this.name = name;
     }

     public Set<User> getUserList() {
          return userList;
     }

     public void setUserList(Set<User> userList) {
          this.userList = userList;
     }
}

User实体的映射文件如下:

<? xml  version = "1.0"  encoding = "utf-8" ?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->

< hibernate-mapping >

      <class name="com.xxx.User" table="tb_user">

            <id name="id" type="java.lang.Long">

                  <column name="ID" />

                  <generator class="native" />

            </id>

            <property name="userName" type="java.lang.String">

                  <column name="UserName" length="20" not-null="true" />

            </property>

            <property name="firstName" type="java.lang.String">

                  <column name="FirstName" length="20" not-null="true" />

            </property>

            <property name="lastName" type="java.lang.String">

                  <column name="LastName" length="20" not-null="true" />

            </property>

            <set name="groupList" inverse="true" table="tb_userGroup">

                  <key>

                        <column name="UserID" not-null="true" />

                  </key>

                  <many-to-many class="com.xxx. Group">

                        <column name="GroupID" />

                  </many-to-many>
            </set>
      </class>
</ hibernate-mapping >
 

Group实体的映射文件如下:

<? xml  version = "1.0"  encoding = "utf-8" ?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->

< hibernate-mapping >

      <class name="com.xxx. Group" table="tb_group">

            <id name="id" type="java.lang.Long">

                  <column name="ID" />

                  <generator class="native" />

            </id>

            <property name="name" type="java.lang.String">

                  <column name="Name" length="20" />

            </property>

            <set name="userList" table="tb_userGroup">

                  <key>

                        <column name="GroupID" not-null="true" />

                  </key>

                  <many-to-many class="com.xxx.User">

                        <column name="UserID" not-null="true"/>

                  </many-to-many>
            </set>
      </class>
</ hibernate-mapping >
 
那么查询在某个Group的User集合的QBC写法为:

      public Set<User> getUnBelongUserList(Long groupId) {

            DetachedCriteria userCriteria = DetachedCriteria

                        .forClass(TmsUser.class);
            userCriteria.createAlias("groupList",  "groupList" , Criteria.LEFT_JOIN);

            Property tempGroupId = Property.forName("groupList.id");

            userCriteria.add(tempGroupId.eq(groupId));

return this.getHibernateTemplate().findByCriteria(userCriteria);

     }
说明:
createAlias 方法不光能为实体属性取别名,他同时也会立即加载该属性,相当于setFetchMode方法。这样写后Hibernate会关联去关联tb_userGroup和tb_group表
用面向对象的思想,通过User实体下的Group集合groupList.id来匹配查询条件,这样写后,Hibernate会判断groupList集合中,哪一个group的id等于参数groupId

  

生成的Sql语句大概为:

    select
        *
    from
        tb_user this_
    left outer join
        tb_userGroup grouplist3_
            on this_.ID=grouplist3_.UserID
    left outer join
        tb_group grouplist1_
            on grouplist3_.GroupID=grouplist1_.ID
    where
     grouplist1_.ID=?

你可能感兴趣的:(Hibernate,职场,qbc,休闲,多对多)