HQL select子句中的相关子查询

时间:2013-10-18 11:05:08

标签: mysql sql hibernate hql correlated-subquery

我正在使用以下HQL:

  

选择新地图(hdr.order_number为order_number,(选择计数(dtl)   来自OrderDetail dtl,其中dtl.user_doc_id = hdr.user_doc_id AND   dtl.status<>来自OrderHeader hdr的'Canceled')as activeRowCount   其中hdr.user_doc_id在('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

我无法理解我的错误。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

以下查询应该有效。只需将hdr1.id替换为表格的主键列(如果不是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')

答案 1 :(得分:0)

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