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语句怎么写?谢谢!

阅读 1.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题