我正在尝试使用morphia做一个简单的findOne()。我的代码如下:
public static void main(String[] args)
{
MongoClient client = new MongoClient();
Morphia morphia = new Morphia();
morphia.map(Restaurant_M.class);
Datastore ds = morphia.createDatastore(client, "test");
System.out.println(ds.find(Restaurant_M.class).get());
client.close();
}
我打印出一个null。我无法找到什么问题。有人能指出我正确的方向吗?感谢。
EDIT 收集格式
{
"_id" : ObjectId("572eb5df1d739cc73c21f953"),
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
@Entity class
@Entity("restaurants")
public class Restaurant_M
{
@Id
public ObjectId _id;
@Property("borough")
public String town;
public String cuisine;
public String name;
@Property("restaurant_id")
public String r_id;
答案 0 :(得分:0)
问题是您没有提供有关该实体的所有信息 你可以用来映射到实体: dt.getCollection(Restaurant_M.class);然后使用DBObject 要么 dt.createQuery(Restaurant_M.class).field("")。equal("指定")。get;
答案 1 :(得分:0)
我按如下方式进行了DBO实现:
public Restaurant_M getByID (String id)
{
Query<Restaurant_M> query = createQuery().field("cuisine").equal(id);
return query.get();
}
并更新了主要方法,如下所示
public static void main(String[] args)
{
MongoClient client = new MongoClient("127.0.0.1:27017");
Morphia morphia = new Morphia();
Datastore ds = morphia.createDatastore(client, "test");
RestaurantDAO rdao = new RestaurantDAOImpl(Restaurant_M.class, ds);
Restaurant_M r = rdao.getByID("Hamburgers");
System.out.println(r);
r = ds.find(Restaurant_M.class).get();
System.out.println (r);
}