I have 3 tables : BookInfo, AuthorInfo and BookAuthors
BookInfo has one to many relationship with BookAuthors. Mapping in BookInfo.hbm.xml :
<set name="bookAuthorsesByBookId" inverse="true">
<key>
<column name="book_id" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="com.pojo.hibernate.BookAuthors"/>
</set>
BookAuthors contain bookInfo and authorInfo. Mapping in BookAuthors.hbm.xml :
<composite-id mapped="true" class="com.pojo.hibernate.BookAuthors">
<key-many-to-one name="bookInfoByBookId" class="com.pojo.hibernate.BookInfo" column="book_id"/>
<key-many-to-one name="authorInfoByAuthorId" class="com.pojo.hibernate.AuthorInfo" column="author_id"/>
</composite-id>
And, relationship between AuthorInfo and BookAuthors is one-to-many,too. Mapping in AuthorInfo.hbm.xml :
<set name="bookAuthorsesByAuthorId" inverse="true">
<key>
<column name="author_id" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="com.pojo.hibernate.BookAuthors"/>
</set>
Now, i'm trying to get all books which has author name = "authorname" using detachedcriteria like this :
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BookInfo.class,"bookInfo");
detachedCriteria.createAlias("bookInfo.bookAuthorsesByBookId","bookAuthorses");
detachedCriteria.createAlias("bookAuthorses.authorInfoByAuthorId", "authorInfo");
detachedCriteria.add(Restrictions.like("authorInfo.authorName", searchQuery, MatchMode.ANYWHERE));
hibernateTemplate.findByCriteria(detachedCriteria);
But, it generates an error. Summarized error :
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select this_.book_id as book1_7_2_,....]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'authorinfo2_.author_name' in 'where clause'
and the query it generates is :
select this_.book_id as book1_7_2_, ... from book.book_info this_
inner join book.book_authors authors1_ on this_.book_id=authors1_.book_id
where (author2_.author_name like ?)
Full Query : *
SELECT this_.book_id AS book1_7_2_,
this_.isbn_10 AS isbn2_7_2_,
this_.isbn_13 AS isbn3_7_2_,
this_.book_title AS book4_7_2_,
this_.book_subtitle AS book5_7_2_,
this_.book_description AS book6_7_2_,
this_.no_of_pages AS no7_7_2_,
this_.book_mrp AS book8_7_2_,
this_.book_language AS book9_7_2_,
this_.book_format AS book10_7_2_,
this_.book_img_url AS book11_7_2_,
this_.publishing_date AS publishing12_7_2_,
this_.verified AS verified7_2_,
this_.publisher_id AS publisher14_7_2_,
publisher3_.publisher_id AS publisher1_19_0_,
publisher3_.publisher_name AS publisher2_19_0_,
bookauthor1_.book_id AS book1_5_1_,
bookauthor1_.author_id AS author2_5_1_
FROM book.book_info this_
INNER JOIN book.publisher_info publisher3_
ON this_.publisher_id = publisher3_.publisher_id
INNER JOIN book.book_authors bookauthor1_
ON this_.book_id = bookauthor1_.book_id
WHERE ( ( this_.book_title LIKE ?
OR authorinfo2_.author_name LIKE ?
OR publisher3_.publisher_name LIKE ?
OR this_.isbn_10 LIKE ?
OR this_.isbn_13 LIKE ? )
AND 1 = 1 )