您的位置:首页 > 数据库 > MySQL

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'XXX' in 'whe

2015-06-17 21:17 579 查看
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'XXX' in 'where clause'

  在写HQL查询时遇到的异常,其它方式的数据库查询出现这个异常的原因应该是一样的       

出现这个问题的原因:

where子句的参数与数据库中的数据类型不匹配

        

举例说明:

session.createQuery("from User u where u.username=" +  user.getUsername()).list();此时使用user.getUsername()返回一个用户名字符串

但这样写报错,其对应sql查询语句为:

Hibernate: select user0_.id as id1_, user0_.username as username1_, user0_.password as password1_ from user user0_ where user0_.username=Haha

正确的查询sql查询语句应为为:select user0_.id as id1_, user0_.username as username1_, user0_.password as password1_ from user user0_ where user0_.username=’Haha‘

        即此时对应的正确的HQL查询语句应为:session.createQuery("from User u where u.username=" +  "'" + user.getUsername() + "'").list();

问题解决

        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息