Home » Support » Forum » JPQL keyword in entity - what to do?

JPQL keyword in entity - what to do?

#1

Hi,

I have a problem with entity that is using one of JPQL keywords as one of the fields. It's a 'type' field. When I try to build a query that looks for example like this:

select new ResultData(p.type.name, count(p.id))
from Product as p group by p.type.name

I got this exception:

com.objectdb.o._PersistenceException:
  Unexpected query token 'type': Identifier is expected
at com.objectdb.o.JPE.g(JPE.java:132) ~[objectdb.jar:na]
at com.objectdb.o.ERR.f(ERR.java:59) ~[objectdb.jar:na]
at com.objectdb.o.OBC.onObjectDBError(OBC.java:1386) ~[objectdb.jar:na]
at com.objectdb.jpa.JpaQuery.getResultList(JpaQuery.java:571) ~[objectdb.jar:na]

There is no problem with this query when I choose for testing another field which is called 'supplier'. So, Is there any possibility to escape such a field name? Or do I have to change this field name and remember no to use words like from, group and so on in my entities?

#2

It seems as an ObjectDB bug since you should be able to use JPQL keywords as field names.

Please try build 2.1.0_01 that should fix it.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

I did some tests and fix is almost working - there is no more exception with unexpected query token, but now I got this one:

com.objectdb.o.InternalException: null
at com.objectdb.o.InternalException.f(InternalException.java:236) ~[objectdb.jar:na]
at com.objectdb.o.QRC.D(QRC.java:551) ~[objectdb.jar:na]
at com.objectdb.o.QRC.C(QRC.java:500) ~[objectdb.jar:na]
at com.objectdb.o.QRC.B(QRC.java:475) ~[objectdb.jar:na]
at com.objectdb.o.QRC.A(QRC.java:427) ~[objectdb.jar:na]
at com.objectdb.o.QRC.u(QRC.java:162) ~[objectdb.jar:na]
at com.objectdb.o.QRM.UR(QRM.java:242) ~[objectdb.jar:na]
at com.objectdb.o.MST.UR(MST.java:866) ~[objectdb.jar:na]
at com.objectdb.o.WRA.UR(WRA.java:286) ~[objectdb.jar:na]
at com.objectdb.o.WSM.UR(WSM.java:113) ~[objectdb.jar:na]
at com.objectdb.o.WRA.UR(WRA.java:286) ~[objectdb.jar:na]
at com.objectdb.o.WSN.UR(WSN.java:417) ~[objectdb.jar:na]
at com.objectdb.o.STC.r(STC.java:418) ~[objectdb.jar:na]
at com.objectdb.o.SHN.ah(SHN.java:468) ~[objectdb.jar:na]
at com.objectdb.o.SHN.J(SHN.java:146) ~[objectdb.jar:na]
at com.objectdb.o.HND.run(HND.java:133) ~[objectdb.jar:na]
at java.lang.Thread.run(Thread.java:619) ~[na:1.6.0_23]

when using query like this:

  TypedQuery<ResultData> q = em.createQuery(
      "select new pl.hplxtool.model.ResultData(p.type.name, count(p.id)) " +
      "from Product as p group by p.type.name", ResultData.class);
  List<ResultData> res = q.getResultList();

ResultData class looks like this:

public class ResultData {
    public String name;
    public Long all;

    public ResultData(String prodType, Long all) {
        this.name = prodType;
        this.all = all;
    }
}

I did run Doctor before test, after changing odb version.

 

#4

Thank you for your report.

There was another unrelated bug in handling new expressions in result queries.

Please try build 2.1.0_02.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

Almost OK, but now I get exception like this:

Caused by: com.objectdb.o.UserException: Failed to build result of type 'pl.hplxtool.model.ResultData': cannot set value 'p.type.name'
at com.objectdb.o.MSG.d(MSG.java:61) ~[objectdb.jar:na]
at com.objectdb.o.ORB.r(ORB.java:157) ~[objectdb.jar:na]
at com.objectdb.o.ORB.j(ORB.java:120) ~[objectdb.jar:na]
at com.objectdb.o.QRR.b(QRR.java:166) ~[objectdb.jar:na]
at com.objectdb.jpa.JpaQuery.getResultList(JpaQuery.java:573) ~[objectdb.jar:na]

It looks like ODB is trying to use setter/getter for this field, but there is no one for this field. Am I right, or I'm doing something wrong? Code is exactly the same as in previous example.

 

 

#6

It seems that the constructor is not found - try changing the 2nd argument from Long to long.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#7

Works perfectly now.

#8

For last few hours I was playing with above query and got another exception after modification to this form:

  TypedQuery<ResultData> q = em.createQuery(
          "select new pl.hplxtool.model.ResultData(p.type.name, p.done, count(p.id)) from Product as p group by p.type.name,p.done",
          ResultData.class);
  List<ResultData> res = q.getResultList();

p.done is boolean type. ResultData was modified like this:

public class ResultData {
 public String name;
 public long all;
 public boolean done;

 public ResultData(String prodType, boolean done, long all) {
  this.name = prodType;
  this.all = all;
  this.done = done;
 }
}

Exception that is thrown:

com.objectdb.o.InternalException: null
 at com.objectdb.o.InternalException.f(InternalException.java:236) ~[objectdb.jar:na]
 at com.objectdb.o.REG.M(REG.java:669) ~[objectdb.jar:na]
 at com.objectdb.o.REG.J(REG.java:549) ~[objectdb.jar:na]
 at com.objectdb.o.REG.K(REG.java:570) ~[objectdb.jar:na]
 at com.objectdb.o.REG.f(REG.java:503) ~[objectdb.jar:na]
 at com.objectdb.o.VOB.j(VOB.java:188) ~[objectdb.jar:na]
 at com.objectdb.o.GQI.Us(GQI.java:152) ~[objectdb.jar:na]
 at com.objectdb.o.PRG.aa(PRG.java:584) ~[objectdb.jar:na]
 at com.objectdb.o.QRM.UR(QRM.java:256) ~[objectdb.jar:na]
 at com.objectdb.o.MST.UR(MST.java:866) ~[objectdb.jar:na]
 at com.objectdb.o.WRA.UR(WRA.java:286) ~[objectdb.jar:na]
 at com.objectdb.o.WSM.UR(WSM.java:113) ~[objectdb.jar:na]
 at com.objectdb.o.WRA.UR(WRA.java:286) ~[objectdb.jar:na]
 at com.objectdb.o.WSN.UR(WSN.java:417) ~[objectdb.jar:na]
 at com.objectdb.o.STC.r(STC.java:418) ~[objectdb.jar:na]
 at com.objectdb.o.SHN.ah(SHN.java:468) ~[objectdb.jar:na]
 at com.objectdb.o.SHN.J(SHN.java:146) ~[objectdb.jar:na]
 at com.objectdb.o.HND.run(HND.java:133) ~[objectdb.jar:na]
 at java.lang.Thread.run(Thread.java:619) ~[na:1.6.0_23]

And btw - sorry for doulbe posts - the proxy that I'm using here is just broken...

#9

I just tried a similar group by / new query but no exception was thrown.

Could you upload a test program that reproduces this issue?

 

Update: Build 2.1.0_03 fixes all these issues.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login