-->
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.  [ 13 posts ] 
Author Message
 Post subject: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Wed Oct 14, 2015 3:14 pm 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Code:
public long count() {

....

String countQuery = "SELECT COUNT(t.id) FROM " + shortClassName + " t ";

TypedQuery<Long> query = __entityManager.createQuery(countQuery, Long.class);

Long count = query.getSingleResult();

return(count);

}


The exception stack is below --

java.lang.IndexOutOfBoundsException: Index: 0
at java.util.Collections$EmptyList.get(Collections.java:4454)
at org.hibernate.ogm.query.impl.OgmQueryLoader.listOfArrays(OgmQueryLoader.java:105)
at org.hibernate.ogm.query.impl.OgmQueryLoader.list(OgmQueryLoader.java:73)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
at org.hibernate.ogm.query.impl.OgmQueryTranslator.list(OgmQueryTranslator.java:127)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)

The entity is as follows :

Code:
@Entity
@Table(name="XXXXX"
)
public class XXXBean implements Serializable {
   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

....

}


If I replace COUNT by MAX or AVG (with suitable modification of the generic type in the TypedQuery<X>), it works fine !

Is there anything obvious, I am missing here ?


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 15, 2015 7:37 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Any clues ?


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 15, 2015 8:50 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
aggregated functions are currently not supported in OGM when you use JP-QL (http://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch07.html#using_jp_ql), but I think in this case we should throw a better exception message.

Could you tell us which database you are using and which version of Hibernate OGM?

Davide

EDIT: syntax


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 15, 2015 8:59 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Thanks for the reply. I am using MongoDB and my hibernate ogm version is 4.2.0.Final . However I observed that some aggregate functions are working fine e.g. MAX / MIN & AVG .

Regards,
Rajesh


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 15, 2015 10:42 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Quote:
However I observed that some aggregate functions are working fine e.g. MAX / MIN & AVG .

I suspect it won't return the correct results, though. How many elements do you have in the DB?

I've created an issue: https://hibernate.atlassian.net/browse/OGM-926

In the meanwhile, you can follow the suggestion in this post: https://forum.hibernate.org/viewtopic.php?f=31&t=1033703

I hope this help,
Davide


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Fri Oct 16, 2015 6:10 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Hi Davide,

Thanks. It really helps. Will still look forward for the fix to come in so that the queries remain platform agnostic.

Regards,
Rajesh


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 22, 2015 3:42 pm 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Tried implementing the count query using native query. Still facing some issues:

1. The documented way of obtaining the count -

Code:
Query query = __entityManager.createNativeQuery(queryStr);
Object[] count = (Object[]) query.getSingleResult();


is throwing ClassCastException . If I simply change it to

Code:
Query query = __entityManager.createNativeQuery(queryStr);
Long count = (Long) query.getSingleResult();


then it is working as expected. Is there a change in 4.2.0.Final w.r.t the mongo query result set behavior ?

2. The parser is unable to parse logical "OR" operation. The query string below , works from command line but not in the code snippet

Code:
String queryStr = "db.collection_name.count( { $or: [ { 'attribute_name' : '1001' }, { 'attribute_name' : '1019' } ] } )";
Query query = __entityManager.createNativeQuery(queryStr);


The stack is as follows:

Code:
com.mongodb.util.JSONParseException:
db.collection_name.count( { $or: [ { 'attribute_name' : '1001' }, { 'attribute_name' : '1019' } ] } )
^
   at com.mongodb.util.JSONParser.parse(JSON.java:208)
   at com.mongodb.util.JSONParser.parse(JSON.java:155)
   at com.mongodb.util.JSON.parse(JSON.java:93)
   at com.mongodb.util.JSON.parse(JSON.java:74)
   at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:48)
   at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:725)
   at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:128)
   at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:191)
   at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.getCustomQuery(NativeNoSqlQueryInterpreter.java:50)
   at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.createQueryPlan(NativeNoSqlQueryInterpreter.java:45)
   at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:226)
   at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:307)
   at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130)
   at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
   at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)


Can you please confirm whether this is supported or not ?


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Sat Oct 24, 2015 11:38 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Any update on my last post ?

Thanks.


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Mon Oct 26, 2015 7:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Sorry, somehow I missed your last question.

Logical operator are not supported at the moment.

I'll create an issue for that


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Mon Oct 26, 2015 7:06 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
The link to the JIRA
https://hibernate.atlassian.net/browse/OGM-927

Would you like to create a patch for this? That would be very appreciated.


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Wed Oct 28, 2015 4:38 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Hello Davide,

Thanks for the update. Although I would love to contribute in the community, I need to obtain lot of legal clearance from my employer before I can participate. I am not sure whether it will work out but I'll definitely try.

Did you get a chance to look into my other question on the return type for native count query ?

Thanks,
Rajesh


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Wed Oct 28, 2015 11:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi rajesh_ghosh,

Actually, it turns out that you can already write that query, but you have to surround the operator with single or double quotes

The query will work:

Code:
db.collection_name.count( { '$or': [ { 'attribute_name' : '1001' }, { 'attribute_name' : '1019' } ] } )


I hope this workaround will work.

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: Simple COUNT Query throwing IndexOutOfBoundsException
PostPosted: Thu Oct 29, 2015 8:21 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Hello Davide,

Yes it worked ! Thanks a bunch. I should tried that out myself.

Regards,
Rajesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.