gpt4 book ai didi

hibernate - 我如何处理 HQL,多对多?

转载 作者:行者123 更新时间:2023-12-03 21:22:55 24 4
gpt4 key购买 nike

enter image description here

这是我的数据库结构。多对多的关系。我想选出写过同一本书的作者。使用 SQL,我做到了。我如何处理 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 和 Author 并且 Book 具有 author 属性:

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

关于hibernate - 我如何处理 HQL,多对多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10855930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com