日期:2014-05-18  浏览次数:20870 次

SQL语法异常,求教求帮助
Dao层

@Override
public Page findOrdersByUser(String username, Date date, Integer page) {

String hql = "select * from order where userid = (select id from user where username = "
+ username + ")";

// if (date != null) {
// hql += " and date = '" + date + "'";
// }

return this.findBySQL(hql, page);
}

Dao里面的私有方法

/**
 * 分页
 */
private Page findBySQL(String hql, int pageNum) {

int totalPage = 1;// 总页数
Page page = new Page();
// 总条数
int countNum = this.getSession().createSQLQuery(hql).addEntity(Order.class).list().size();

// 总页数
if (countNum % Constants.PAGE_NUMBER == 0) {
totalPage = countNum / Constants.PAGE_NUMBER;
} else {
totalPage = countNum / Constants.PAGE_NUMBER + 1;
}
page.setTotalPage(totalPage);

List<Order> result = this.getSession().createSQLQuery(hql).addEntity(Order.class)
.setFirstResult((pageNum - 1) * Constants.PAGE_NUMBER)
.setMaxResults(Constants.PAGE_NUMBER).list();
page.setResult(result);
page.setCurrPage(pageNum);
return page;
}

异常信息

Hibernate: 
    select
        * 
    from
        
    order where
        userid = (
            select
                id 
            from
                user 
            where
                username = 1
        )
2014-3-19 9:30:46 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [action] in context with path [/Dentist] threw exception [org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order where userid = (select id from user where username = 1)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.ref