我定义了一个类:HibernateBooleanDemo
只有两个属性:id和enabled,分别是String和boolean类型,分别对应表中的id和isEnabled列。并编写了相应的get和set方法和DAO类。但是如下代码很奇怪:
String hql = "from HibernateBooleanDemo where isEnabled=false";
在上面的查询语句中写列名即isEnabled时可以正常运行。但是写属性enabled时,却抛出异常,大概意思是说不能解析该属性。
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();
一直不明白是什么原因。请大家指点
【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