问题描述
项目中用到别人创建的数据库,不能修改数据库结构,商品表中有个销售价格字段 sell_price decimal(11,2),要对它进行比较查询,大致就是要查价格处于什么范围的商品,这里以大于或等于为例(即最低价格)。Condition类未查询条件类,里面有id和minPrice等属性。我用hibernate3操作数据库,报出如下错误,请知道解决办法的仁兄不灵赐教,不胜感激。实现代码public List<Products> getProductsList(Condition condition){String hql = "FROM Products p WHERE 1=1 AND p.pid=:id AND p.sell_price >= :min" Query query = getSession().createQuery(hql);query.setLong("id", condition.getId());query.setBigDecimal("min", condition.getMinPrice());//query.setDouble("min", condition.getMinPrice().floatValue());}Condition c = new Condition(); c.setId(2); BigDecimal min= new BigDecimal("20.00"); c.setMinPrice(min);错误内容Exception in thread "main" org.hibernate.QueryException: could not resolve property: sell_price of: com.boling.model.pojo.Products [FROM com.boling.model.pojo.Products p WHERE 1=1 AND p.pid=:id AND p.sell_price >= :min] at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44) at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38) at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1358) at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279) at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386) at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:566) at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241) at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188) 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:728) at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216) at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041) at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3645) at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762) at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1690) 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) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623) at com.boling.model.impl.ProductManagerImpl.getProductsList(ProductManagerImpl.java:130) at com.boling.model.impl.ProductManagerImpl.main(ProductManagerImpl.java:225)Java Result: 1 问题补充:ekian 写道
解决方案
要把decimal类型的sell_price 转换成float类型的,才可以