views:

2230

answers:

10

I have this Java code (JPA):

String queryString = "SELECT b  , sum(v.votedPoints) as votedPoint " +
                       " FROM Bookmarks b  " +
                         " LEFT OUTER JOIN Votes v " +
                           " on (v.organizationId = b.organizationId) " + 
                         "WHERE b.userId = 101  " + 
                         "GROUP BY b.organizationId " +
                         "ORDER BY votedPoint ascending ";
EntityManager em = getEntityManager();
Query query = em.createQuery(queryString);
query.setFirstResult(start);
query.setMaxResults(numRecords);
List results = query.getResultList();

I don't know what is wrong with my query because it gives me this error:

java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
        at org.hibernate.hql.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1802)
        at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1420)
        at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
        at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
        at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
        at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
        at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:271)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
        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:94)
        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:1650)

Thanks.

+2  A: 

Stab in the dark - Are you sure you have a consistent set of jars - perhaps you need to get the antlr jar that comes with the hibernate distribution you are using...

Chris Kimpton
+1  A: 

May be you have some double-quotes " missing or which should be doubled in your HQL.

Illustration here.

Or you miss some simple quotes as illustrated there

VonC
A: 

Hi, I have the consistent set of jars because simple queries like this one

"SELECT b  FROM table_name b  WHERE b.userId = 102 " 

are working. I have verified all double quotes and everything is alright.

My database is: mysql, and I use jpa to connect to it. I don't know what is causing the problem. Maybe this type of join, i don't know

Ionut
A: 

Er, isnt your query trying to select b which is a table alias and thats not allowed as far as I know.

Gowri
A: 

I'd probably guess that something is going wrong with your query, because the method HqlBaseParser fails to lookup is called recover(RecognitionException, Bitset). Perhaps this query fails for some reason the other simpler queries don't (and the NoSuchMethod exception is thrown when attempting to recover from that error).

java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
JesperE
A: 

The query seems to be invalid unless it's an artifact of formatting.

I think you meant this:

Select b, ...

to be:

Select b.organizationId, ...

??

nicerobot
+1  A: 

I've found the problem: because this is a native query, java classes for this 2 tables must have some special attributes:
in Bookmarks.java class

@OneToMany(mappedBy = "bookmarkId")
private Collection votesCollection;
and in Votes.java class

@JoinColumn(name = "bookmark_id", referencedColumnName = "bookmark_id")
@ManyToOne
[private Bookmarks bookmarkId;

and i have also changed the query to work


tring queryString = "SELECT b, sum(v.votedPoints) " +
                          "FROM Bookmarks b  " +
                          "LEFT OUTER JOIN b.votesCollection v " + 
                          "WHERE b.userId = 101  " + 
                          "GROUP BY b.organizationId " +
                          "ORDER BY sum(v.votedPoints) asc ";

thanks for the help

Ionut
A: 

Your query is still wrong. Maybe it works with your driver/db but it isn't standard SQL. You should be selecting b.* or b.organizationId.

nicerobot
A: 

Hi Thanks for the help :) the query is good, this is not Sql standard but hibernate query language.

Ionut
+1  A: 

You definitely have an issue with the version of hibernate and ANTLR jars that you are using. The recover method wasn't present in the ANTLR Parser class until version 2.7.6? If you are using an earlier version of ANTLR, such as 2.7.2, then you will see this problem.

Using maven can cause this sort of situation, where you depend on Hibernate and its transitive dependencies, but something 'closer'; e.g. Struts; providers a different, earlier version of ANTLR and that earlier version gets resolved in your application.

If you can provide the version of jars involved, we would be able to help some more. Once you have fixed the issue with the jar versions, you should get a more revealing error message which shows what is wrong with your HQL expression.

jabley