首页 > hibernate5.06,两实体关系为双向多对一,一端对象取特定的多端对象,hql如何写?

hibernate5.06,两实体关系为双向多对一,一端对象取特定的多端对象,hql如何写?

People 表
---------------
id |   
---------------
 1 |
--------------

Book 表
-----------------------------
id |   type   |  _people_id (Fk)
-----------------------------
 1 |    1     |      1
-----------------------------
 2 |    1     |      1
-----------------------------
 3 |    2     |      1
-----------------------------
 4 |    3     |      1
-----------------------------


// People.hbm.xml>
// <set name="bookSet" cascade="all" lazy="false">
//   <key column="h_people_id"/>
//   <one-to-many class="Book"/>
// </set>
public class People{
   private int id;
   private Set<Book> bookSet;
   public People(){}
   public People(int id,Set<Book> bookSet)
   {
      this.id=id;
      this.bookSet=bookSet;
    }
   //getter & setter
}

//Book.hbm.xml > 
//<many-to-one name="people" column="h_people_id" cascade="none" not-null="true"></many-to-one>
public class Book{
   private int id;
   private int type;
   private People people;

   public Book(){}   
   //getter & setter
}

// dao层的方法 根据id获取用户及其 book.type=1的书
public People getPeopleById(int userId,int bookId)
{
   String hql="???????";
   
   return *省略*.uniqueResult();
}

取id=1的用户a,a包含type=1的两本书,hql语句怎么写?谢谢!

【热门文章】
【热门文章】