Morphia 与我的映射 JAVA Class
Morphia mapping with my JAVA Class
我正在尝试使用吗啡做一个简单的 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();
}
我打印出一个空值。我无法找到出了什么问题。有人能指出我正确的方向吗?谢谢。
编辑
Collection格式
{
"_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"
}
@实体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;
问题是您没有提供有关实体所需的所有信息
您可以只使用映射到实体:
dt.getCollection(Restaurant_M.class);然后使用 DBObject
要么
dt.createQuery(Restaurant_M.class).field("").equal("to specify").get;
我做了一个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);
}
我正在尝试使用吗啡做一个简单的 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();
}
我打印出一个空值。我无法找到出了什么问题。有人能指出我正确的方向吗?谢谢。
编辑 Collection格式
{
"_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"
}
@实体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;
问题是您没有提供有关实体所需的所有信息 您可以只使用映射到实体: dt.getCollection(Restaurant_M.class);然后使用 DBObject 要么 dt.createQuery(Restaurant_M.class).field("").equal("to specify").get;
我做了一个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);
}