-->
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.  [ 11 posts ] 
Author Message
 Post subject: Exception in sort query
PostPosted: Fri Apr 15, 2016 5:44 am 
Newbie

Joined: Wed Apr 13, 2016 8:21 am
Posts: 7
Hi Team,
When i add sort function to the existing query for MongoDB using hibernate ogm im getting java.lang.IllegalArgumentException, yet the same query is running fine in the mongo shell...
Iam using hibernateOGM 5.0.0.CR1...




Quote:
INFO: OGM000001: Hibernate OGM 5.0.0.CR1
Apr 15, 2016 3:04:15 PM org.hibernate.ogm.transaction.impl.OgmJtaPlatformInitiator initiateService
INFO: OGM000076: No explicit or implicit defined JTAPlatform. Using NoJtaPlatform
java.lang.IllegalArgumentException: Unsupported native query: Invalid input '....', expected WhiteSpace or EOI (line 1, pos 189):
db.UserFactualContent.find( {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public' }]},{ 'DESCRIPTION': 1,'NAME': 1,'TOKEN':1}).sort({'clicks':-1}).limit(1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:863)
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:143)
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:200)
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:210)
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301)
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:593)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:470)


the code is:

Code:
String str ="db.UserFactualContent.find( {'$or': [ {'NAME':{'$regex':'(?i)"
               + "Bangalore"
               + "'}}, {'DESCRIPTION':{'$regex':'(?i)"
               + "Bangalore"
               + "'}}],'$and':[{'FLAG':'public' }]},{ 'DESCRIPTION': 1,'NAME': 1,'TOKEN':1}).sort({'clicks':-1}).limit(1)" ;
         Query content = entityManager
               .createNativeQuery(str,LynkedContentJpa.class);
         List<LynkedContentJpa> url = content.getResultList();


Thanks in advance

--Javed


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Mon Apr 18, 2016 6:30 am 
Hibernate Team
Hibernate Team

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

as stated in the documentation (http://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch11.html#ogm-mongodb-queries-native):

Quote:
No cursor operations such as sort() are supported. Instead use the corresponding MongoDB query modifiers such as $orderby within the criteria parameter.


So, your query should be:
Code:
db.UserFactualContent.find( { '$query' : {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public' }]},{ 'DESCRIPTION': 1,'NAME': 1,'TOKEN':1}, '$orderby' : { 'clicks' : -1 } } )


You can set the limit using
Code:
Query content = entityManager.createNativeQuery(str,LynkedContentJpa.class)
                              .setMaxResults(1);
List<LynkedContentJpa> url = content.getResultList();


That said, I'm trying the $regex operator and it does not seem to work at the moment.

Davide


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Tue Apr 19, 2016 2:57 am 
Newbie

Joined: Wed Apr 13, 2016 8:21 am
Posts: 7
Hi Team,
Iam still facing the same issue even though i changed to $orderby instead of $sort


Code:
String str = "db.UserFactualContent.find( {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public' }]},{ 'DESCRIPTION': 1,'NAME': 1,'TOKEN':1}, '$orderby' : { 'clicks' : -1 } } )";
         Query content = entityManager.createNativeQuery(str,LynkedContentJpa.class).setMaxResults(2);
         List<LynkedContentJpa> url = content.getResultList();


Quote:
INFO: OGM000001: Hibernate OGM 5.0.0.CR1
Apr 19, 2016 12:20:19 PM org.hibernate.ogm.transaction.impl.OgmJtaPlatformInitiator initiateService
INFO: OGM000076: No explicit or implicit defined JTAPlatform. Using NoJtaPlatform
com.mongodb.util.JSONParseException:
db.UserFactualContent.find( {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public' }]},{ 'DESCRIPTION': 1,'NAME': 1,'TOKEN':1}, '$orderby' : { 'clicks' : -1 } } )
^
at com.mongodb.util.JSONParser.parse(JSON.java:230)
at com.mongodb.util.JSONParser.parse(JSON.java:155)
at com.mongodb.util.JSON.parse(JSON.java:92)
at com.mongodb.util.JSON.parse(JSON.java:73)
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:71)
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:866)
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:143)
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:200)
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:210)
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301)
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:593)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:470)




Quote:
Iam using mongo-java-driver:3.2.2 which says $orderby is deprecated. I even worked with $sort modifier but of no use.


Kindly give me a solution,

Thanks in advance.

--Javed


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Tue Apr 19, 2016 4:10 am 
Newbie

Joined: Wed Apr 13, 2016 8:21 am
Posts: 7
Hi,

Code:
db.UserFactualContent.aggregate([{ '$match': {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public'}] } },{ '$unwind' : '$clicks' },{ '$group' : {'_id' : '$_id' ,'clicks' : {$push:'$clicks'} ,'token' : { '$push': '$TOKEN' } } },{ '$sort' : { '_id' : -1 } }])


This query is working fine in mongodb shell but throwing an exception when i access it through java hibernate ogm native query...

Quote:
INFO: OGM000076: No explicit or implicit defined JTAPlatform. Using NoJtaPlatform
com.mongodb.util.JSONParseException:
db.UserFactualContent.aggregate([{ '$match': {'$or': [ {'NAME':{'$regex':'(?i)Bangalore'}}, {'DESCRIPTION':{'$regex':'(?i)Bangalore'}}],'$and':[{'FLAG':'public'}] } },{ '$unwind' : '$clicks' },{ '$group' : {'_id' : '$_id' ,'clicks' : {$push:'$clicks'} ,'token' : { '$push': '$TOKEN' } } }])
^
at com.mongodb.util.JSONParser.parse(JSON.java:230)
at com.mongodb.util.JSONParser.parse(JSON.java:155)
at com.mongodb.util.JSON.parse(JSON.java:92)
at com.mongodb.util.JSON.parse(JSON.java:73)
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:71)
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:866)
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:143)
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:200)
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:210)
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301)
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:593)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:470)


Thanks,
-Javed


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Tue Apr 19, 2016 5:03 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
.aggregate() is not supported at the moment.

I've created an issue to keep track of this: https://hibernate.atlassian.net/browse/OGM-1024

Davide


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Tue Apr 19, 2016 5:16 am 
Newbie

Joined: Wed Apr 13, 2016 8:21 am
Posts: 7
Okay..
When can we expect the full fledged working of hibernate ogm?
(or) if such functions like .sort() / .aggregate() doesnt work with nativeQueries, is there any other way to access them??


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Mon Apr 25, 2016 8:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
> When can we expect the full fledged working of hibernate OGM?

Good question, I don't have an answer to this.

I've tried to propose this issue to some contributors but nobody started working on it yet.
One of us might decide to tackle this issue but if you want to speed up the process
you can try to vote for it on the JIRA or propose a solution yourself, that would be much appreciated.


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Thu Jul 07, 2016 5:46 pm 
Hi, I have had the same problem :-/

My environment is:
WildFly 10
Hibernate OGM 5.0.1.final

If I use a simple query like:
Code:
Query query = em.createNativeQuery("db.teste.find({'person_id':{'$ne':" + personId + "}})", Teste.class);


Everything works. But if I try a little complex query :
Code:
Query query = em.createNativeQuery("db.tracker.find({'$and':[{'deleted':{'$eq':false}}, {'person_id':{'$ne':26677}})", Teste.class);



It throws the following exception:

Quote:
18:33:36,590 ERROR [stderr] (default task-37) org.bson.json.JsonParseException: JSON reader was expecting a value but found 'db'.
18:33:36,591 ERROR [stderr] (default task-37) at org.bson.json.JsonReader.readBsonType(JsonReader.java:228)
18:33:36,591 ERROR [stderr] (default task-37) at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:343)
18:33:36,591 ERROR [stderr] (default task-37) at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:136)
18:33:36,591 ERROR [stderr] (default task-37) at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:61)
18:33:36,591 ERROR [stderr] (default task-37) at com.mongodb.BasicDBObject.parse(BasicDBObject.java:75)
18:33:36,591 ERROR [stderr] (default task-37) at com.mongodb.BasicDBObject.parse(BasicDBObject.java:62)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parse(MongoDBQueryDescriptorBuilder.java:99)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:78)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:870)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:143)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:200)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.getCustomQuery(NativeNoSqlQueryInterpreter.java:50)
18:33:36,592 ERROR [stderr] (default task-37) at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.createQueryPlan(NativeNoSqlQueryInterpreter.java:45)
18:33:36,593 ERROR [stderr] (default task-37) at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:210)
18:33:36,593 ERROR [stderr] (default task-37) at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301)
18:33:36,593 ERROR [stderr] (default task-37) at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130)
18:33:36,593 ERROR [stderr] (default task-37) at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
18:33:36,593 ERROR [stderr] (default task-37) at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
(...)


Top
  
 
 Post subject: Re: Exception in sort query
PostPosted: Mon Oct 31, 2016 2:14 pm 
Newbie

Joined: Mon Oct 31, 2016 2:08 pm
Posts: 1
Any ideas on how to do an order by/sort? $orderby was deprecated in MongoDB in v3.2, and .sort() is not supported in Hibernate OGM 5.0.2.

Also, it's not quite clear to me why native queries don't just simply "work". I resorted to using a native query because of a limitation of JPA support (I think it was OGM-825). If any one can provide some literature on this I would appreciate it.


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Sun Feb 12, 2017 11:57 pm 
Newbie

Joined: Sun Feb 12, 2017 11:28 pm
Posts: 2
hi

currently the bug which was reported seem to be resolved in 5.x version. https://hibernate.atlassian.net/browse/OGM-1024

I was trying to perform an aggregate function for a native query.

db.user_interaction.aggregate([ {'$unwind':'$page_session'}, {'$unwind': '$page_session.click'}, {'$match' : {'page_session.click.resource_name':'campaign'}}, {'$group':{ '_id':'$page_session.click.resource_id', 'averageSessionDuration': {'$avg':'$page_session.timespent'}, 'viewerCount':{'$sum':1} }}])

I get the error

I am not sure if i am missing something here.

Caused by: org.bson.json.JsonParseException: JSON reader was expecting a value but found 'db'.
at org.bson.json.JsonReader.readBsonType(JsonReader.java:237) ~[mongo-java-driver-3.4.2.jar:?]
at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:345) ~[mongo-java-driver-3.4.2.jar:?]
at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:138) ~[mongo-java-driver-3.4.2.jar:?]
at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:61) ~[mongo-java-driver-3.4.2.jar:?]
at com.mongodb.BasicDBObject.parse(BasicDBObject.java:75) ~[mongo-java-driver-3.4.2.jar:?]
at com.mongodb.BasicDBObject.parse(BasicDBObject.java:62) ~[mongo-java-driver-3.4.2.jar:?]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parse(MongoDBQueryDescriptorBuilder.java:99) ~[hibernate-ogm-mongodb-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:78) ~[hibernate-ogm-mongodb-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:862) ~[hibernate-ogm-mongodb-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:143) ~[hibernate-ogm-mongodb-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:200) ~[hibernate-ogm-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.getCustomQuery(NativeNoSqlQueryInterpreter.java:50) ~[hibernate-ogm-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.createQueryPlan(NativeNoSqlQueryInterpreter.java:45) ~[hibernate-ogm-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:210) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301) ~[hibernate-ogm-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130) ~[hibernate-ogm-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]


Top
 Profile  
 
 Post subject: Re: Exception in sort query
PostPosted: Wed Feb 15, 2017 9:49 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
It should be solved but we haven't released it yet.

Which version are you using?


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