Hi ,
l have a java.sql.SQLException,
Code:
org.springframework.jdbc.BadSqlGrammarException:
Hibernate operation: could not execute query;
bad SQL grammar [
select distinct
this_.BOOK_ID as y0_,
this_.TITLE as y1_,
this_.ISBN as y2_ from BOOKS this_
inner join LINK_BOOK_AUTHOR authors5_ on this_.BOOK_ID=authors5_.BOOK_ID
inner join AUTHORS author3_ on authors5_.AUTHOR_ID=author3_.AUTHOR_ID
inner join NOTES note1_ on this_.BOOK_ID=note1_.NOTE_ID
inner join PUBLISHERS publisher2_ on this_.PUBLISHER_ID=publisher2_.PUBLISHER_ID
where (lower(y2_) like ?) and (1=1) and (1=1) and (1=1) order by y1_ asc];
nested exception is java.sql.SQLException: Unknown column 'y2_' in 'where clause'
java.sql.SQLException: Unknown column 'y2_' in 'where clause'
...
...
it throw when l executed the method below ,
Code:
public Collection findBooksByCriteriaWithLimit(BookCommand bookCommand)
throws DataAccessException {
final Book book = bookCommand.getBook();
final Author iAuthor = bookCommand.getAuthor();
final Publisher iPublisher = book.getPublisher();
final Note iNote = book.getNote();
final String sortColumn = bookCommand.getS_sortColumn();
final int page = bookCommand.getS_page();
final int pageSize = bookCommand.getS_pageSize();
final int ascending = bookCommand.getS_ascending();
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Example exampleBook = Example.create(book).ignoreCase()
.enableLike(MatchMode.ANYWHERE);
Example examplePublisher = Example.create(iPublisher).ignoreCase()
.enableLike(MatchMode.ANYWHERE);
Example exampleNote = Example.create(iNote).ignoreCase()
.enableLike(MatchMode.ANYWHERE);
Example exampleAuthor = Example.create(iAuthor).ignoreCase()
.enableLike(MatchMode.ANYWHERE);
Criteria bookCriteria = session.createCriteria(Book.class)
.setProjection(Projections.distinct(Projections.projectionList()
.add(Projections.property("id"),"id")
.add(Projections.property("title"),"title")
.add(Projections.property("isbn"),"isbn")
));
if (ascending > 0) {
bookCriteria.addOrder(Order.asc(sortColumn));
} else {
bookCriteria.addOrder(Order.desc(sortColumn));
}
bookCriteria.add(exampleBook);
bookCriteria.createCriteria("note").add(exampleNote);
bookCriteria.createCriteria("publisher").add(examplePublisher);
bookCriteria.createCriteria("authors").add(exampleAuthor);
bookCriteria.setFirstResult(page * pageSize)
.setMaxResults(pageSize);
return bookCriteria.setResultTransformer(new AliasToBeanResultTransformer(Book.class)).list();
}
});
}
l found the error was came from the generated sql by hibernate , or make it simple , the problem can be restated more simple as below ,
when l execute the findBooksByCriteriaWithLimit(..) , BadSqlGrammarException throw , the generated sql would be ,
Code:
select
this_.BOOK_ID as y0_, this_.TITLE as y1_, this_.ISBN as y2_
from BOOKS this_
where y0_ = 1
it suppose to be ,
Code:
select
this_.BOOK_ID as y0_, this_.TITLE as y1_, this_.ISBN as y2_
from BOOKS this_
where this_.BOOK_ID = 1
Is findBooksByCriteriaWithLimit(..) a wrong implementation or l miss something ?
RelationShip of Tables :
Books <-- 1:1 --> Notes
Books <-- M:M --> Authors
Books <-- M:1 --> Publishers
Env :
Hibernate 3.1
Mysql 4.1.8-nt
thanks.
moon