我定义了一个类:HibernateBooleanDemo 只有两个属性:id和enabled,分别是String和boolean类型,分别对应表中的id和isEnabled列。并编写了相应的get和set方法和DAO类。但是如下代码很奇怪: String hql = "from HibernateBooleanDemo where isEnabled=false". Session s = HibernateSessionFactory.getSession(). Transaction t = s.beginTransaction(). Query q = s.createQuery(hql). List list = q.list(). System.out.println(list.size()). System.out.println(((HibernateBooleanDemo)list.get(0)).isEnabled()). t.commit(). HibernateSessionFactory.closeSession(). 在上面的查询语句中写列名即isEnabled时可以正常运行。但是写属性enabled时,却抛出异常,大概意思是说不能解析该属性。 一直不明白是什么原因。请大家指点 【AmethystWish】: 若"from HibernateBooleanDemo where isEnabled=false".写为"from HibernateBooleanDemo where enabled=false".错误如下: aused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column enabled in where clause at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3124) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1149) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1262) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2145) ... 8 more
【liandti】: from HibernateBooleanDemo h where h.enabled=false 如果用属性就加别名
【AmethystWish】: 查询字符串改为:"from HibernateBooleanDemo h where h.enabled=false".之后运行,结果如下: Exception in thread "main" org.hibernate.QueryException: could not resolve property: enabled of: org.acman.demo.HibernateBooleanDemo [from org.acman.demo.HibernateBooleanDemo h where h.enabled=false] at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)