开发者

How to get id of model saved in mongodb using java driver or morphia?

开发者 https://www.devze.com 2023-03-15 06:41 出处:网络
There is a tutorial http:开发者_运维知识库//code.google.com/docreader/#p=morphia&s=morphia&t=QuickStart

There is a tutorial

http:开发者_运维知识库//code.google.com/docreader/#p=morphia&s=morphia&t=QuickStart

But it leaves out the detail how to get the id of the hotel.

Morphia morphia = ...;
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);

I thinks its either java driver for mongo or morphia API. So how do I get the id?


This is no different from how you would do it, say you were using a RDBMS.

Let's say you are building a travel site. Now you have a database which already has the hotels that are associated with the cities that they belong to.

Now I as a user come in and request to be shown all the hotels, in, new York, then you would go to the database and query for all hotels that are in new York and then send back a list from your application, maybe rendered into a page and then display the results to the user. This results page, will at least have, the id thats in the database, and the hotel name. Now, once the user wants to see more details, say of The Waldorf hotel, they would click on that, and the I'd for that hotel would be passed back to your application. Now the code sample you have can be then used to load all the details of the hotel a Nd then display them to the user.

Make sense?

To summarize, the I'd of the record you want to load, typically comes from user input, where you load a list of Ida and the user can then select one or more. And to get the list of Ida, you typically query by some parameters, in the above case, by the city name.

Now, to proceed with using the sample above, you can hard code the id, to a value that you already have in the db and complete the code.

Another option, you can create a new instance of the hotel object and then issue the ave command like the docs say and then once you do that, if you have a property mapped as an ID, you can do a get on that property and then pass it along to the the note I'd, and then execute the read query.

0

精彩评论

暂无评论...
验证码 换一张
取 消