Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Native query on MongoDB don't work with find(Trying to do native query on a mongodb with find leads to exception)
Native query on MongoDB don't work with find [message #1009524] Fri, 15 February 2013 09:22 Go to next message
jm collin is currently offline jm collinFriend
Messages: 7
Registered: February 2013
Junior Member
Hello,

I'm using EclipseLink 2.4.0 with MongoDB 2.2. Due to preceeding error (cf. http://www.eclipse.org/forums/index.php/t/453373/), I try to make native query to turnaround limitation. But the result is not good.

Native query using find are refused on mongoDB side.
The findOne command is send to Mongo as :
query: { $eval: "db.attachmentRequest.findOne({ "EMITTERACCOUNT__id" : "511DDC4517800A501FA9FA40" })", args: {} }  ntoreturn: -1 ntoskip: 0


and the result is correct. But if you replace directly on mongo console findOne by find you will get :
{ retval: { value: "DBQuery: clouderialDevDB.attachmentRequest -> undefined" }, ok: 1.0 }


I was able to to a find on EclipseLink side using this :
Query q = em.createNativeQuery("function() { var ret=[]; var cursor = db.attachmentRequest.find(); while (cursor.hasNext()) { ret.push(cursor.next()); } return ret; }", AttachmentRequest.class);


This returns an Array of documents, but EclipseLink can't parse this result :
q.getResultList();

always give a exception :
Local Exception Stack:
Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [EISMappedRecord(
	3 => {EMITTERACCOUNT__id=511DDDAC17808B8294346869, _id=511DDDAC17808B829434686A, CLOSETIME=0, REQUESTTIME=1360911780000, STATUS=0, PAYERACCOUNT__id=511DDDAC17808B8294346868}
	2 => {EMITTERACCOUNT__id=511DDC4517800A501FA9FA40, _id=511DDC4517800A501FA9FA41, CLOSETIME=0, REQUESTTIME=1360911420000, STATUS=0, PAYERACCOUNT__id=511DDC4517800A501FA9FA3F}
	1 => {EMITTERACCOUNT__id=511D326517806B8E1472CA17, _id=511D326517806B8E1472CA18, CLOSETIME=0, REQUESTTIME=1360867920000, STATUS=0, PAYERACCOUNT__id=511D326517806B8E1472CA16}
	10 => {EMITTERACCOUNT__id=511DF7701780143B28CBA76C, _id=511DF7701780143B28CBA76D, CLOSETIME=0, REQUESTTIME=1360918380000, STATUS=0, PAYERACCOUNT__id=511DF76F1780143B28CBA76B}
	0 => {EMITTERACCOUNT__id=511D31BC178076CC9DCC2203, _id=511D31BC178076CC9DCC2204, CLOSETIME=0, REQUESTTIME=1360867740000, STATUS=0, PAYERACCOUNT__id=511D31BC178076CC9DCC2202}
	7 => {EMITTERACCOUNT__id=511DF6C9178046852A6380B1, _id=511DF6C9178046852A6380B2, CLOSETIME=0, REQUESTTIME=1360918200000, STATUS=0, PAYERACCOUNT__id=511DF6C9178046852A6380B0}
	6 => {EMITTERACCOUNT__id=511DF6AF1780270A11F699BB, _id=511DF6AF1780270A11F699BC, CLOSETIME=0, REQUESTTIME=1360918140000, STATUS=0, PAYERACCOUNT__id=511DF6AE1780270A11F699BA}
	5 => {EMITTERACCOUNT__id=511DF66117803B563A858F21, _id=511DF66117803B563A858F22, CLOSETIME=0, REQUESTTIME=1360918080000, STATUS=0, PAYERACCOUNT__id=511DF66017803B563A858F20}
	4 => {EMITTERACCOUNT__id=511DF53A1780B5BAF50F220A, _id=511DF53A1780B5BAF50F220B, CLOSETIME=0, REQUESTTIME=1360917780000, STATUS=0, PAYERACCOUNT__id=511DF53A1780B5BAF50F2209}
	9 => {EMITTERACCOUNT__id=511DF73617801FC86025F423, _id=511DF73617801FC86025F424, CLOSETIME=0, REQUESTTIME=1360918320000, STATUS=0, PAYERACCOUNT__id=511DF73517801FC86025F422}
	8 => {EMITTERACCOUNT__id=511DF6F21780BC1B4DFA1279, _id=511DF6F21780BC1B4DFA127A, CLOSETIME=0, REQUESTTIME=1360918200000, STATUS=0, PAYERACCOUNT__id=511DF6F21780BC1B4DFA1278}
	13 => {EMITTERACCOUNT__id=511DFAFA17801AC3EA825C98, _id=511DFAFA17801AC3EA825C99, CLOSETIME=0, REQUESTTIME=1360919280000, STATUS=0, PAYERACCOUNT__id=511DFAF917801AC3EA825C97}
	11 => {EMITTERACCOUNT__id=511DF7A31780A83CFD4AFC94, _id=511DF7A31780A83CFD4AFC95, CLOSETIME=0, REQUESTTIME=1360918380000, STATUS=0, PAYERACCOUNT__id=511DF7A31780A83CFD4AFC93}
	12 => {EMITTERACCOUNT__id=511DF7ED178028B216F1FEA9, _id=511DF7ED178028B216F1FEAA, CLOSETIME=0, REQUESTTIME=1360918500000, STATUS=0, PAYERACCOUNT__id=511DF7ED178028B216F1FEA8})] during the execution of the query was detected to be null.  Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=AttachmentRequest )


So my question now : does someone knows how to parse a array of Entity returning by a native query ?

JM.

[Updated on: Fri, 15 February 2013 09:24]

Report message to a moderator

Re: Native query on MongoDB don't work with find [message #1064441 is a reply to message #1009524] Wed, 19 June 2013 09:37 Go to previous messageGo to next message
Michael Hunziker is currently offline Michael HunzikerFriend
Messages: 4
Registered: June 2013
Junior Member
@jm_collin:

Were you able to resolve your issues above?
I'm having the same problem Wink

Before that one I had the following error, which obviously was also posted by you Wink http://www.eclipse.org/forums/index.php/t/453373/




Re: Native query on MongoDB don't work with find [message #1065384 is a reply to message #1064441] Tue, 25 June 2013 14:03 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

What is the exact JPA code used for the native query?

The code should be like,

For your previous issue you can define a @Basic for the foreign key field to allow querying on it (mark it as insertable/updateable=false).

Or the following JPQL should work,

"Select ar from AttachmentRequest ar where ar.emitterAccount = :emitter"

where the parameter is an EmitterAccount with the id.




James : Wiki : Book : Blog : Twitter
Re: Native query on MongoDB don't work with find [message #1074144 is a reply to message #1065384] Fri, 26 July 2013 07:34 Go to previous messageGo to next message
jm collin is currently offline jm collinFriend
Messages: 7
Registered: February 2013
Junior Member
Sorry for the late response.


The native query I try was : "db.attachmentRequest.find()". But this query returns a Cursor and not lines.

So to get lines I try this native query which returns lines :
Quote:
function() { var ret=[]; var cursor = db.attachmentRequest.find(); while (cursor.hasNext()) { ret.push(cursor.next()); } return ret; }


The complete code is :
Quote:
Query q = em.createNativeQuery("function() { var ret=[]; var cursor = db.attachmentRequest.find(); while (cursor.hasNext()) { ret.push(cursor.next()); } return ret; }", AttachmentRequest.class);
q.getResultList();

When getting the result EclipseLink throw me an Exception like listed below.

Quote:
For your previous issue you can define a @Basic for the foreign key field to allow querying on it (mark it as insertable/updateable=false).

I don't understand what you said about @Basic. Can you provide me an example ?

Many thanks for your help.
Re: Native query on MongoDB don't work with find [message #1077409 is a reply to message #1074144] Thu, 01 August 2013 16:58 Go to previous messageGo to next message
John Bracken is currently offline John BrackenFriend
Messages: 2
Registered: August 2013
Junior Member
Hi Jim,

What James is suggesting is that you define a mapping on your entity like:

@Basic
@Column(name = "EMITTERACCOUNT__id", updatable = false, insertable = false)
private String emmiterAccountId;


This would allow you to have the following JPQL query which in theory should net the same result as your native query:

"Select ar from AttachmentRequest ar where ar.emitterAccount = :emitter"

Let me know if this helps. If not I can investigate the native query issue further.

Thanks, John
Re: Native query on MongoDB don't work with find [message #1077985 is a reply to message #1077409] Fri, 02 August 2013 11:44 Go to previous message
jm collin is currently offline jm collinFriend
Messages: 7
Registered: February 2013
Junior Member
Unfortunately because I had to make it work, i don't any piece of code available for a test. When I will be able to test this I will put the result on this thread. You will have to be patient.
Previous Topic:MongoDB queries throwing NPE
Next Topic:Eclipselink 2.4 migration to openJPA (WebSphere 8.5.5 javax.persistence)
Goto Forum:
  


Current Time: Tue Apr 23 11:50:57 GMT 2024

Powered by FUDForum. Page generated in 0.03568 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top