我的应用程序正在使用objectify。我对NoSql很陌生。
我有这样的数据模型。不要注意缺少getter和setter,缺乏构建器模式等等,这只是一个例子。
正如您所看到的,ReallyWeirdCar是一个非常深的对象图的根。
现在,假设我使用给定的方法在内存中完全构建了一个ReallyWeirdCar对象。
另外,假设数据存储完全为空。
如何使用objectify保存该对象?
.save().entity().entity(Rwc1)是否足以一次性保存整个对象图?
我怎么能坚持这样的关系?
此外,如果大多数时候我都在执行诸如“查找客户john所要求的所有汽车”之类的查询时,您会认为这是一个“很好”的模型吗?
thx预先
@Entity
class ReallyWeirdCar {
@Id
public String id;
public String name;
@Load
public Ref<Engine> e1;
@Load
public Ref<Engine> e2;
// a reference to the customer who solicited the construction of this car
@Index
@Load
public Ref<Customer> customer;
}
@Entity
class Customer {
@Id
public String id;
public String name;
}
@Entity
class Engine {
@Id
public String id;
public String name;
@Load
public Ref<List<Carburetor>> ecs;
}
@Entity
class Carburetor {
@Id
public String id;
public String name;
@Load
public Ref<Manufacturer> manufacturer;
}
@Entity
class Manufacturer {
@Id
public String id;
public String name;
}
// inside some service
public buildAndPersistCar() {
Customer cust1 = new Customer("cust1", "customer1");
Manufacturer m1 = new Manufacturer("m1", "manufacturer1");
Carburetor c1 = new Carburetor("carb1", "carburetor1", m1);
Carburetor c2 = new Carburetor("carb2", "carburetor2", m1);
Carburetor c3 = new Carburetor("carb3", "carburetor3", m1);
Carburetor c4 = new Carburetor("carb4", "carburetor4", m1);
Engine e1 = new Engine("e1", "engine1", Arrays.asList(c1,c2));
Engine e2 = new Engine("e2", "engine2", Arrays.asList(c3,c4)));
ReallyWeirdCar rwc1 = new ReallyWeirdCar("rwc1", "reallyweirdcar1", e1, e2, cust1);
// what do i do here ????
// how do i persist rwc1 ???
}发布于 2015-06-01 04:24:51
在对象化中没有“级联保存”的概念。如果要保存N个实体,则必须显式地保存它们。你救的是你救的。
从性能的角度来看,这看起来不太理想。GAE数据存储喜欢更胖的粗粒度实体;您的示例需要四轮获取才能到达制造商。如果这是一个真正准确的模型,那么您可能希望接受它;四轮抓取不会杀死您,特别是大多数实体都在memcache中。但是,如果你能去奥马尔模型(比如说,在发动机中嵌入化油器),你可能会使它更快。
这与您在关系数据库中所面临的问题完全相同(规范化或非规范化)。使用Objectify实际上更容易,因为Objectify对于“轮”批获取是明智的;您将得到四个API调用,而不是~N^4 API调用。但是,装上一整台装满数千个部件的发动机仍然是痛苦的。
https://stackoverflow.com/questions/30563855
复制相似问题