0

BookInfo、AuthorInfo、BookAuthors の 3 つのテーブルがあります。

BookInfo は BookAuthors と 1 対多の関係にあります。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 の関係も 1 対多です。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 ) 
4

0 に答える 0