使用带有collections属性的detachedcriteria时,'where子句'中的未知列

时间:2013-08-13 22:32:12

标签: hibernate hibernate-criteria detachedcriteria

我有3个表:BookInfo,AuthorInfo和BookAuthors

BookInfo与BookAuthors有一对多的关系。 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包含bookInfo和authorInfo。 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>

而且,AuthorInfo和BookAuthors之间的关系也是一对多的。 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>

现在,我正在尝试使用detachedcriteria获取所有具有作者姓名=“authorname”的书籍,如下所示:

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);

但是,它会产生错误。 汇总错误:

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'

,它生成的查询是:

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 ?)

完整查询:*

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 ) 

0 个答案:

没有答案