我需要将以下HQL转换为CRITERIA:

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)

最佳答案

我很想知道对b.cadimp.cdCadimp的限制存在问题。

您在cadimp.cdCadimp上写了一个限制,但我猜标准不知道它是cadimp中的一个属性。因此,您应该为该属性创建一个别名,以允许条件遍历。

首先,删除开始时的限制。

然后,最后添加:

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


我建议在最后添加它,因为此createCriteria不会返回主要条件(用于方法链接),而是返回属性中的条件。

09-10 14:57