日期:2014-05-18 浏览次数:20880 次
List<Category> categorys= session.
createQuery("select b.categorySet from Book b where b.id=:id").setLong("id", bookId).list();
List<Book> books= session.
createQuery("select c.bookSet from Category c where c.id=:id").setLong("id", categoryId).list();
------解决方案--------------------
确认category里面有吗,检查一下你的控制台打出的sql语句,看看正确与否
如果都正确,看下系统是否报异常,把异常贴上来看看
------解决方案--------------------
查询属于某个Category的所有Book:
select book from Book book,Category category where category = all elements(book.categorys) and category.categoryId=?
查询某个Category中有多少本书
select count(*) from Book book,Category category where category = all elements(book.categorys) and category.categoryId=?
这其实用的是SQL的all函数。
关于elements在Hibernate reference 的 Hql 里有介绍。