I have ManyToMany relationship between Author and Book .
table : AUTHOR
id | name
table : ASSIGN
*id | author_id | book_id*
table : BOOK
id | title
But I am using not standart ManyToMany mapping, I am using next
class Book
{
@Id
long id;
String title;
@OneToMany
List<Assign> authors;
}
class Assign
{
@Id
long id;
@ManyToOne
Book book
@ManyToOne
Author author;
}
class Author
{
@Id
long id;
String name;
@OneToMany
List<Assign> books;
}
What is query to get all Books for Author's name? Also What is query to get all Books' names for Author's name?
You simply have two OneToMany associations. You just need to use joins in your query, as described in the Hibernate documentation over HQL .
select book from Author author
left join author.books assign
left join assign.book book
where author.name = :name
The second query is the same, except you just want the book names:
select book.name from Author author ...
Side note: you shouldn't name your collection of Assigns books
or authors
: it's very confusing. Name it assigns
.
What is query to get all Books for Author's name?
from Book b order by b.authors.author.name
What is query to get all Books' names for Author's name?
select b.title from Book b order by b.authors.author.name
Have you swapped title and name in Author and Book classes?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.