数据存储中的持久关系 - App Engine 和 objectify

persisting relationships in datastore - app engine and objectify

我的应用正在使用 objectify。我是 NoSql 的新手。

我有一个这样的数据模型。不注意缺少 getter 和 setter,缺少构建器模式等只是一个例子。

如您所见,ReallyWeirdCar 是一个相当深的对象图的根。

现在,假设我使用给定的方法在内存中完全构建了一个 ReallyWeirdCar 对象。

另外,假设数据存储完全为空。

如何使用 objectify 保存该对象?

y().save().entity(rwc1) 是否足以一次性保存整个对象图?

我如何维持这样的关系?

另外,如果我大部分时间都在执行 "find all the cars that were solicited by customer john"

这样的查询,你会认为这是一个 "good"(性能)模型吗

提前谢谢

    @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 ???



}

Objectify 中没有"cascading save" 的概念。如果要保存 N 个实体,则必须显式保存它们。 您节省的就是您节省的

从性能的角度来看,这看起来不太理想。 GAE 数据存储喜欢更胖的粗粒度实体;您的示例需要四轮获取才能到达制造商。如果这是一个真正准确的模型,那么您可能希望接受它;四轮获取不会杀死你,尤其是大多数实体都在 memcache 中。但是,如果您可以对模型进行非规范化(例如,在引擎中嵌入化油器),您可能会使其更快。

这与您在关系数据库(规范化或非规范化)中遇到的问题完全相同。使用 Objectify 实际上更容易,因为 Objectify 很聪明 "rounds" 批量获取;您将收到四个 API 电话,而不是 ~N^4 API 个电话。但是加载一个包含数千个组件的整个引擎仍然很痛苦。