我定義了一個類: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)[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(DotNode.Java:539)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.Java:221)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.Java:172)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.Java:94)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.Java:90)
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.Java:725)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.Java:1215)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.Java:4032)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.Java:3518)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.Java:1758)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.Java:776)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.Java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.Java:281)
【AmethystWish】:
如果屬性是其它類型的,如字符串類型的,運行就沒有問題,如果是bool、boolean類型的就這樣,不知道為什麼。初學hibernante,應該有不知道的地方
【AmethystWish】:
已解決。錯誤總結:類映射文件中
元素的name屬性對應類中屬性的名字,要以小寫字母開頭。我使用HibernateSynchronizer工具自動生成類映射文件,name屬性全部以大寫字母開頭。
但是很奇怪,其它類型的屬性沒有問題,只有布爾類型的有問題。