[AJUDA] IF Statement JPA

2 respostas
W

Boa tarde, estou precisando de ajuda, pois não consigo utilizar a condição IF dentro de uma estrutura JPQL. Isso é possível?

SELECT p FROM Product p , ProductPrice pp WHERE p.id=pp.product.id AND pp.id = (IF ((SELECT COUNT(tpp.id) FROM ProductPrice tpp WHERE tpp.startDate <= :subDateStart AND tpp.endDate >= :subDateEnd AND tpp.product.id = p.id) > 0) THEN (SELECT tpp.id FROM ProductPrice tpp WHERE tpp.startDate <= :subDateStart2 AND tpp.endDate >= :subDateEnd2 AND tpp.product.id = p.id) ELSE (SELECT tpp.id FROM ProductPrice tpp WHERE tpp.main= :subMain AND tpp.product.id = p.id)) AND pp.value > :productValuePriceStart GROUP BY p.id

Obrigado.

2 Respostas

B

Não utilizo muito HQL, mas creio que isso não é possível.

O teu caso deve ser resolvido utilizando CASE … WHEN, como podes visualizar aqui:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions

Boa sorte!

W

Tentei essa solução porem começo a receber o seguinte erro. Parece que nao existe essa expressao em JPQL

QUERY:

SELECT p FROM Product p , ProductPrice pp WHERE p.id = pp.product.id AND pp.id = (CASE WHEN ((SELECT COUNT(tpp.id) FROM ProductPrice tpp WHERE tpp.startDate <= :subDateStart AND tpp.endDate >= :subDateEnd AND tpp.product.id = p.id) > 0) THEN (SELECT tpp.id FROM ProductPrice tpp WHERE tpp.startDate <= :subDateStart2 AND tpp.endDate >= :subDateEnd2 AND tpp.product.id = p.id) ELSE (SELECT tpp.id FROM ProductPrice tpp WHERE tpp.main= :subMain AND tpp.product.id = p.id) END) AND pp.value > :productValuePriceStart GROUP BY p.id

ERRO:

x.AllServiceTests

x.ProductServiceTest

testFindAllProductsForSale(x.ProductServiceTest)

java.lang.NullPointerException

at org.hibernate.hql.ast.tree.CaseNode.getDataType(CaseNode.java:40)

at org.hibernate.hql.ast.tree.BinaryLogicOperatorNode.extractDataType(BinaryLogicOperatorNode.java:232)

at org.hibernate.hql.ast.tree.BinaryLogicOperatorNode.initialize(BinaryLogicOperatorNode.java:59)

at org.hibernate.hql.ast.HqlSqlWalker.prepareLogicOperator(HqlSqlWalker.java:1160)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4330)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1909)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1837)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1834)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:824)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:610)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)

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.(HQLQueryPlan.java:101)

at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)

at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)

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:1769)

at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)

at x.ProductDAOImpl.createFilterQuery2(ProductDAOImpl.java:322)

at x.ProductDAOImpl.findProductsByAllFilters(ProductDAOImpl.java:94)

at x.ProductServiceImpl.findProductsByAllFiltersForSale(ProductServiceImpl.java:48)

at br.com.jwweb.ecommerce.modulogeral.service.ProductServiceTest.testFindAllProductsForSale(ProductServiceTest.java:108)

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 junit.framework.TestCase.runTest(TestCase.java:168)

at junit.framework.TestCase.runBare(TestCase.java:134)

at junit.framework.TestResult$1.protect(TestResult.java:110)

at junit.framework.TestResult.runProtected(TestResult.java:128)

at junit.framework.TestResult.run(TestResult.java:113)

at junit.framework.TestCase.run(TestCase.java:124)

at junit.framework.TestSuite.runTest(TestSuite.java:232)

at junit.framework.TestSuite.run(TestSuite.java:227)

at junit.framework.TestSuite.runTest(TestSuite.java:232)

at junit.framework.TestSuite.run(TestSuite.java:227)

at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)

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)

Alguma idéia?

obrigado

Criado 21 de janeiro de 2011
Ultima resposta 21 de jan. de 2011
Respostas 2
Participantes 2