Hibernate '中的未知列;其中第'条;将detachedcriteria与collections属性一起使用时
我有3个表:BookInfo、AuthorInfo和BookAuthors BookInfo与BookAuthors有一对多关系。 BookInfo.hbm.xml中的映射:Hibernate '中的未知列;其中第'条;将detachedcriteria与collections属性一起使用时,hibernate,hibernate-criteria,detachedcriteria,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-man
<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>
<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>
但是,它会产生一个错误。
汇总错误:
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 )
这不是你要找的问题。未知列“authorInfo.author\u name”表示查询在
where
子句中具有该值。基本上,您提供的sql与您的错误不匹配。只要找到hibernate抱怨的查询,您就会发现字段author\u name
在不正确的表中被搜索。查询是相同的。我仍然添加了完整的查询。我不知道为什么它使用authorinfo2_uu2;alias,尽管它没有生成该别名。那么,如果直接在sql工具中执行查询(添加参数值),查询是否有效?Hibernate日志发布从数据库检索的内容。如果将该查询日志放入SQL工具中,它们也应该可以工作。只要试着在日志中找到不起作用的字段,看看查询中有什么失败。当我添加:internal JOIN book.author\u info authorinfo2\uon bookauthor1.author\u id=authorinfo2.author\u id
时,它就起作用了。但是,我不知道如何使用分离的条件添加它。这就是createAlias
所做的,基本上,它执行一个连接操作。你基本上拥有的是一个多对多的关系,但在我看来,你把它映射为一个不必要的一对多关系。映射双向多对多关系,让书与作者以及作者与书直接在实体中,不是更容易吗?只是设计一件事。
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 )