Java Entity Manager - JSON reader was expecting a value but found 'db'
Solution 1
The thing is, if the type of the field you want to access is a String
you will need to add quotation marks to the value of the field.
To give you an example, I've built a little DSL for creating queries without having to bother with the MongoDB specific keywords, such as $eq
, $lt
etc. and I had a similar problem. It can be solved by adding single quotation marks.
Queries.get(Query.where("name", Operator.is("'Kim'")))
Solution 2
This answer is late, but I was battling with the same issue for ages so perhaps it'll help someone else.
As per this forum post, the keys and values must both be enclosed in quotation marks, so your something__id
should be "something__id"
.
Admin
Updated on June 05, 2022Comments
-
Admin almost 2 years
I have this code already working:
EntityManager entityManager = getEntityManager(); entityManager.getTransaction().begin(); String query = "db.Band.find({})"; List<Band> list = (List<Band>) entityManager.createNativeQuery(query, Band.class).getResultList(); entityManager.close(); return list;
It returns a
List
with no problem. Now I want to sort thatList
bydate
:EntityManager entityManager = getEntityManager(); entityManager.getTransaction().begin(); String query = "db.Band.find({something__id:ObjectId(\"" + myId + "\")}).sort({\"somethingelse.date\":-1})"; List<Band> list = (List<Band>) entityManager.createNativeQuery(query, Band.class).getResultList(); entityManager.close(); return list;
My console gives me this message:
org.bson.json.JsonParseException: JSON reader was expecting a value but found 'db'
I checked that my
String
was working properly in Mongo console and it did. Any ideas?Edit: I tried putting "something__id" between quotes because it wouldn't work even without the sort part if I didn't (I just realised that), but now it says I am making an unsupported native query, pointing out the Sort part. Should I sort it after I already have it in a List object?