1

I am executing a unit test and keep getting this error for the following code:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp  WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate ) OR ( t1.sdate <= sysdate AND t1.edate is null ) AND t1r.Test1Id = t1.Test1Id AND t1.relTpId = tp.TypeId and t1.isActv = 1 AND t1r.isActv = 1 AND tp.shrtCode = :shrtCode AND t1r.custId = :custId]. 
[69, 163] The expression is not a valid conditional expression.
[164, 330] The query contains a malformed ending.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1512)
    at com.etiya.mts.data.dao.newpac.dao.ProductOfferDAO.findTest1(ProductOfferDAO.java:821)
    at com.etiya.mts.data.dao.newpac.service.ProductOfferService.findTest1(ProductOfferService.java:241)
    at com.etiya.mts.rules.productoffering.Test1Controller.execute(Test1Controller.java:36)
    at com.etiya.mts.ejb.BasketValidateBean.validateBasket(BasketValidateBean.java:108)
    at com.etiya.mts.ejb.RuleManager.validateBasket(RuleManager.java:887)
    at com.etiya.mts.ejb.BasketValidateBeanTest.validaBasketTest(BasketValidateBeanTest.java:210)
    at com.etiya.mts.ejb.BasketValidateBeanTest.basketValidateBeanTest(BasketValidateBeanTest.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    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)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp  WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate ) OR ( t1.sdate <= sysdate AND t1.edate is null ) AND t1r.Test1Id = t1.Test1Id AND t1.relTpId = tp.TypeId and t1.isActv = 1 AND t1r.isActv = 1 AND tp.shrtCode = :shrtCode AND t1r.custId = :custId]. 
[69, 163] The expression is not a valid conditional expression.
[164, 330] The query contains a malformed ending.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:150)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:325)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:270)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:157)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:138)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:112)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:98)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:82)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1510)
    ... 31 more
String cqString = "SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp" +
                "  WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate )" +
                " OR ( t1.sdate <= sysdate AND t1.edate is null )" +
                " AND t1r.Test1Id = t1.Test1Id" + 
                " AND t1.relTpId = tp.TypeId" +
                " and t1.isActv = 1" +
                " AND t1r.isActv = 1" + 
                " AND tp.shrtCode = :shrtCode" +
                " AND t1r.custId = :custId";
        Query cq = getEntityManager().createQuery(cqString); 
        cq.setParameter("shrtCode", ofrRuleTpShrtCode);
        cq.setParameter("custId", custId);

If I change the code to this:

StringBuilder query = new StringBuilder();
        query.append("SELECT * FROM Test1 t1,Test1_REL t1r,Type tp ");
        query.append("WHERE (sysdate between t1.SDATE and t1.EDATE) OR (t1.sdate <= sysdate AND t1.edate is null) ");
        query.append("AND t1r.CUST_OFR_RULE_ID=t1.CUST_OFR_RULE_ID ");
        query.append("AND t1.REL_TP_ID=tp.GNL_TP_ID and t1.IS_ACTV=1 ");
        query.append("AND t1r.IS_ACTV=1 AND tp.SHRT_CODE = ? AND t1r.CUST_ID = ? ");
        Query cq = getEntityManager().createNativeQuery(query.toString()); 
        cq.setParameter(1, shrtCode);
        cq.setParameter(2, custId);

I don't get any exception. The strange thing is this code works in other environments, but not mine. It looks like a syntax error, but I keep getting this error in other parts of the code where createQuery is used.

I checked the maven's repository but it is up to date. Does any one have any opinion about this issue?

Edit: I changed the eclipselink version from 2.4.0 to 2.5.0 but still getting the same error. Can it be something like a language setting of windows?

enter image description here

lamostreta
  • 2,359
  • 7
  • 44
  • 61
  • 3
    `sysdate` is a native database function (in Oracle, I think?) that cannot directly be embedded in JPQL. Shouldn't it? – Tiny Dec 04 '14 at 15:36
  • I used it in createNativeQuery also it works fine, the problem was in the above code.. – lamostreta Dec 04 '14 at 15:38
  • 1
    That above code snippet implies that it is JPQL because you are using the `createQuery()` method - `getEntityManager().createQuery(cqString)`. JPQL is unaware of the native database function `sysdate`. – Tiny Dec 04 '14 at 15:43
  • 1
    @Tiny is right, replace `sysdate` with `CURRENT_DATE` (which is a [JPA function](https://docs.oracle.com/html/E24396_01/ejb3_overview_query.html#ejb3_overview_query_functions)) and see if it works (with JPA query, not native query). – Predrag Maric Dec 04 '14 at 15:43
  • sorry, I pasted the wrong code, originally it was current_date. I am trying again with the current_date. – lamostreta Dec 04 '14 at 15:48
  • same result. It may be a bug as @PredragMaric suggested.. – lamostreta Dec 04 '14 at 15:50

2 Answers2

1

Which EclipseLink version are you using? You might be hitting this bug, it is resolved in version 2.4.3. Looks like the problem is with Hermes query parser.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • maven deals with the project libraries. It should be the latest eclipselink I guess. But I couldn't see in pom.xml. What I see from console is: "Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JPQLException" is that the eclipselink version? – lamostreta Dec 04 '14 at 15:36
  • Yes, you are using 2.4.0. Can you change that to 2.5.2 (this is the latest) in `pom.xml`, or post it into your question? – Predrag Maric Dec 04 '14 at 15:39
  • I changed system language, locale and format information. It worked. I still don't get it how it is related.. – lamostreta Dec 05 '14 at 09:05
1

I changed system language, locale and format information. It worked.

lamostreta
  • 2,359
  • 7
  • 44
  • 61
  • Strange, I also don't know what's the relation, but it's probably how eclipselink handles `current_date`. You might experiment with passing date as a parameter instead of `current_date`, to see if it changes anything. Anyway, great that it crossed your mind to try changing system date settings :) – Predrag Maric Dec 05 '14 at 09:28