سؤال

I am using the following HQL:

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 ('PO1')

There is no association defined between OrderHeader and OrderDetail. When the above query is being executed, I am getting the following error:

    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

I am not able to understand whre I am going wrong. Can someone please help?

هل كانت مفيدة؟

المحلول 2

I am using MAP as Hibernate entity mode. So the property names are case sensitive. My property name is USER_DOC_ID where as I am using user_doc_id and hence Hibernate complains: "could not resolve property: user_doc_id of: OrderHeader"

نصائح أخرى

The below query should work. Just replace hdr1.id with whichever is the the primary key column of the table if not 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')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top