我如何处理HQL,多对多? [英] How do I do with HQL, many to many?

查看:75
本文介绍了我如何处理HQL,多对多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的数据库结构。关系很多。我想挑选一些写过同一本书的作者。用SQL,我做到了。我该怎么做与HQL?

Here is my database structure. Relation many to many. I want to make a selection of authors who have written the same book. With SQL, I did. How do I do with HQL?

   Query query = session.createSQLQuery("SELECT FirstName, LastName FROM authors, books, author_book as ab WHERE ab.authorID=authors.authorID AND ab.bookID = books.bookID AND ab.bookID = :id");
   query.setInteger("id", 1);
   List list = query.list();
   Iterator<Object[]> iter = list.iterator();
   while (iter.hasNext()) {
       Object[] obj = iter.next();
       System.out.println(obj[0] + " " + obj[1]);
    }


推荐答案

假设实体名称是Book和作者,该书有作者属性:

Assuming the entity names are Book and Author and that Book has authors attribute:

select a.firstName, a.lastName from Book b join b.authors a where b.id = :id

这篇关于我如何处理HQL,多对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆