EJB3.0中查询boolean值的方法
时间:2011-04-26
我定义了一个类: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
EJB3.0?D2é?ˉboolean?μμ?·?·¨(2)
ê±??:2011-04-26
??liandti??:
from HibernateBooleanDemo h where h.enabled=false
è?1?ó?ê?D??í?ó±e??
??AmethystWish??:
2é?ˉ×?·?′????a£o"from HibernateBooleanDemo h where h.enabled=false";??oó??DD£??á1?è???£o
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)[Page]
???? at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
???? at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1265)
???? at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
???? at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:372)
???? at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNod
|