Querying Morphia by Id

msciab picture msciab · Dec 17, 2010 · Viewed 14k times · Source

I am using Morphia, the Pojo mapper for MongoDB, and I find difficult a task that in my view should be very simple: getting an object by id. I am able to find all the objects in a collection but I cannot figure out the simple task of querying using an id I got from the list. I am actually talking about the ObjectId. If I try to render it in JSON I see

Answer

Gates VP picture Gates VP · Dec 18, 2010

This question seems incomplete.

It also seems like the answer to you question is on the Morphia QuickStart page. Seems to be as simple as follows.

Datastore ds = morphia.createDatastore("testDB");
String hotelId = ...; // the ID of the hotel we want to load
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId);

So you'll definitely need more details.