-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: ArrayIndexOutOfBoundsException while parsing HQL query
PostPosted: Wed Sep 06, 2017 5:52 am 
Newbie

Joined: Wed Sep 06, 2017 5:41 am
Posts: 3
I have a strange situation where I have a simple HQL query which is failing.

I am unable to find the whats the issue with the code, below is the error followed by the code:

Code:
java.lang.ArrayIndexOutOfBoundsException: 143
at antlr.BaseAST.toString(BaseAST.java:322)
at antlr.BaseAST.toStringList(BaseAST.java:341)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringTree(BaseAST.java:358)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:629)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:663)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:184)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:137)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
at sun.reflect.GeneratedMethodAccessor1360.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:294)
at com.sun.proxy.$Proxy145.createQuery(Unknown Source)


Below is the code:

Code:
List<Object[]> mmReports = this.entityManager.createQuery(
"SELECT dmr.bankCardNumber, dmr.accountNumber, dmr.transactionType, "
+ "sum(dmr.dayCount) from "
+ DMR.class.getName() + " dmr WHERE dmr.month =:month AND dmr.transactionType=:txnType "
+ "GROUP BY dmr.bankCardNumber, dmr.accountNumber, dmr.transactionType")

.setParameter("month", month)
.setParameter("txnType", txnType)
.getResultList()


Top
 Profile  
 
 Post subject: Re: java.lang.ArrayIndexOutOfBoundsException while preparing HQL
PostPosted: Wed Sep 06, 2017 7:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to add a space between = and : when declaring the parameter.

So instead of:

Code:
dmr.month =:month AND dmr.transactionType=:txnType


You should have:

Code:
dmr.month = :month AND dmr.transactionType= :txnType


Also, you shouldn't use String concatenation for building queries. If the parameters are sent from the users, you application risks to be exposed to SQL injection attacks.

Use Criteria API if you want to build queries dynamically.


Top
 Profile  
 
 Post subject: Re: java.lang.ArrayIndexOutOfBoundsException while preparing HQL
PostPosted: Wed Sep 06, 2017 7:23 am 
Newbie

Joined: Wed Sep 06, 2017 5:41 am
Posts: 3
I tried removing the space but that's didn't solve the problem, however if there is some issue with the query there should be a different exception, the exception is really interesting java.lang.ArrayIndexOutOfBoundException, I am not sure if this exception is before the query is fired to the DB or while preparing the query?
Thanks

vlad wrote:
You need to add a space between = and : when declaring the parameter.

So instead of:

Code:
dmr.month =:month AND dmr.transactionType=:txnType


You should have:

Code:
dmr.month = :month AND dmr.transactionType= :txnType


Also, you shouldn't use String concatenation for building queries. If the parameters are sent from the users, you application risks to be exposed to SQL injection attacks.

Use Criteria API if you want to build queries dynamically.


Top
 Profile  
 
 Post subject: Re: ArrayIndexOutOfBoundsException while parsing HQL query
PostPosted: Wed Sep 06, 2017 7:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
I am not sure if this exception is before the query is fired to the DB or while preparing the query?


The exception occurs when the Hibernate ANTLR parses the HQL query to an SQL query.

This is not about preparing the SQL statement which, either is emulated on the JDBC Driver or happening on the database server.

Now, let's get back the acatual problem:

Quote:
I tried removing the space but that doesn't solve the problem


Of course, it does not solve the problem. I told you to add a space, not to remove it.


Top
 Profile  
 
 Post subject: Re: ArrayIndexOutOfBoundsException while parsing HQL query
PostPosted: Wed Sep 06, 2017 8:14 am 
Newbie

Joined: Wed Sep 06, 2017 5:41 am
Posts: 3
Thanks Vlad

I tried adding space as you suggested, the same issue reoccurs, however i have queries in other places where there is no space and query works fine.

vlad wrote:
Quote:
I am not sure if this exception is before the query is fired to the DB or while preparing the query?


The exception occurs when the Hibernate ANTLR parses the HQL query to an SQL query.

This is not about preparing the SQL statement which, either is emulated on the JDBC Driver or happening on the database server.

Now, let's get back the acatual problem:

Quote:
I tried removing the space but that doesn't solve the problem


Of course, it does not solve the problem. I told you to add a space, not to remove it.


Top
 Profile  
 
 Post subject: Re: ArrayIndexOutOfBoundsException while parsing HQL query
PostPosted: Wed Sep 06, 2017 9:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If this happens with Hibernate 5, and you can replicate it using our templates, then you should open a Jira issue.


Top
 Profile  
 
 Post subject: JPA java.lang.ArrayIndexOutOfBoundsException
PostPosted: Thu Sep 07, 2017 5:38 am 
Newbie

Joined: Thu Sep 07, 2017 5:25 am
Posts: 1
at antlr.BaseAST.toString(BaseAST.java:322)
at antlr.BaseAST.toStringList(BaseAST.java:341)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringList(BaseAST.java:343)
at antlr.BaseAST.toStringList(BaseAST.java:347)
at antlr.BaseAST.toStringTree(BaseAST.java:358)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:629)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:663)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:184)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:137)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
at sun.reflect.GeneratedMethodAccessor1527.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:294)
at com.sun.proxy.$Proxy151.createQuery(Unknown Source)


Facing such issue sometimes, not frequently. Can anyone help me to know what could be the possible situation? Thanks.


Top
 Profile  
 
 Post subject: Re: ArrayIndexOutOfBoundsException while parsing HQL query
PostPosted: Thu Sep 07, 2017 8:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
This question has already been asked before. I merged the two into one post. Follow the conversation and come up with a replicating test case, as explained in my previous post.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.