2012-10-04 128 views
2

我需要將下面的HQL轉換爲標準:轉換HQL到標準

Query query = bd.getSession().createQuery(
      "from Booking as b " + 
       " where " + 
       "  b.cadimp.cdCadimp = :cdCadimp AND "+ 
       "  b.dtEntrada >= :dateParam AND " + 
       "  b.idCancelado <> 'S' AND " + 
       "  b.cdNavio <> 141 AND " + 
       "  0 = (select count(*) from Item i2 where " + 
       "   (" + 
       "    i2.pk.cdEmpresa = b.pedido.cdEmpresa " + 
       "    AND i2.pk.nrPedido = b.pedido.nrPedido " + 
       "    AND" + 
       "     (i2.cdTaxa >= 101 " + 
       "     AND i2.cdTaxa < 110) " + 
       "     AND i2.flag = 'R'" + 
       "     AND i2.dtPagto <> '0001-01-01'" + 
       "   )" + 
       "  )" 
       + " ORDER BY b.dtInformacao asc"); 

我的標準:(?任何想法)

Criteria c = getSession() 
       .createCriteria(Booking.class, "b") 
       .add(Restrictions.eq("cadimp.cdCadimp", 
         (Integer) restrictionsValues[0])) 
       .add(Restrictions.gt("dtEntrada", calendar.getTime())) 
       .add(Restrictions.ne("idCancelado", "S")) 
       .add(Restrictions.ne("cdNavio", (short) 141)) 
       .add(Subqueries.eq(
         0, 
         DetachedCriteria.forClass(Item.class, "i") 
           .add(Restrictions.eqProperty("cdEmpresa", "b.pedido.cdEmpresa")) 
           .add(Restrictions.eqProperty("nrPedido", "b.pedido.nrPedido")) 
           .add(Restrictions.conjunction() 
             .add(Restrictions.ge("cdTaxa",101)) 
             .add(Restrictions.lt("cdTaxa", 110)) 
             .add(Restrictions.eq("flag", "R")) 
             .add(Restrictions.ne("dtPagto", defaultDb2Date))) 
             .setProjection(
              Projections 
                .projectionList() 
                .add(Projections.rowCount()) 
           ) 

       )) 
       .addOrder(Order.asc("dtInformacao")); 

我收到以下錯誤:

org.hibernate.HibernateException: Unknown entity: null 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPropertyMapping(CriteriaQueryTranslator.java:638) 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:523) 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:538) 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:543) 
    at org.hibernate.criterion.PropertyExpression.toSqlString(PropertyExpression.java:51) 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419) 
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:110) 
    at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:71) 
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419) 
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:123) 
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:92) 
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:93) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1603) 
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 
    --- 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
+1

您的原始HQL執行沒有問題嗎? –

回答

2

我想冒險的限制超過b.cadimp.cdCadimp有問題。

你寫了一個cadimp.cdCadimp的限制,但我猜Criteria不知道它是cadimp裏面的一個屬性。所以你應該爲該屬性創建一個別名,以允許條件遍歷它。

首先,刪除開始時的限制。

然後,在末尾加上:

createCriteria("cadimp"). // add a criteria over the entity represented by this property 
add(Restrictions.eq(cdCadimp, ...);` 

我建議在末尾添加它,因爲這個createCriteria不超過財產返還(對於方法鏈接)的主要標準,但標準。

+0

無論如何還有其他的事情,我不是很確定...所以這只是一個猜測:) – helios