0

我正在使用以下 HQL:

从 OrderHeader hdr where hdr.user_doc_id in (' PO1')

OrderHeader 和 OrderDetail 之间没有定义关联。执行上述查询时,出现以下错误:

    Caused by: org.hibernate.QueryException: could not resolve property: user_doc_id of: OrderHeader [select new map(hdr.order_number as order_number,(select count(dtl) from OrderDetail dtl where dtl.user_doc_id=hdr.user_doc_id AND dtl.status <> 'Cancelled') as activeRowCount) from OrderHeader hdr where hdr.user_doc_id  in ('PO00000
1')]
        at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
        at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
        at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1451)
        at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:312)
        at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487)
        at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611)
        at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
        at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
        at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
        at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
        at org.hibernate.hql.ast.tree.DotNode.resolveSelectExpression(DotNode.java:674)
        at org.hibernate.hql.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:877)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2049)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.aliasedSelectExpr(HqlSqlBaseWalker.java:2229)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2419)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2116)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1981)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1513)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:292)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
        at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
        at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
        at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
        at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
        at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
        ... 88 more

我无法理解我哪里出错了。有人可以帮忙吗?

4

2 回答 2

0

下面的查询应该可以工作。如果不是 id,只需将 hdr1.id 替换为表的主键列

select new map(
hdr.order_number as order_number, 
(select count(dtl) from OrderHeader hdr1 join OrderDetail dtl on dtl.user_doc_id=hdr1.user_doc_id AND dtl.status <> 'Cancelled' AND hdr1.id = hdr.id) as activeRowCount)
from OrderHeader hdr where hdr.user_doc_id in ('PO1')
于 2013-10-18T11:22:40.093 回答
0

我使用 MAP 作为休眠实体模式。所以属性名称区分大小写。我的属性名称是 USER_DOC_ID,因为我使用的是 user_doc_id,因此 Hibernate 抱怨:“无法解析属性:user_doc_id of: OrderHeader”

于 2013-10-21T06:59:43.387 回答