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 that List by date:
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?
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With