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

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();


问题解决

        

你可能感兴趣的:(Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'XXX' in 'whe)