【问题标题】:Holding different instances of the same class in GAE Datastore在 GAE 数据存储中保存同一类的不同实例
【发布时间】:2011-04-04 21:31:30
【问题描述】:

我知道这个标题非常混乱。

您好,我在这里遇到了一些奇怪的问题。

我有一个属性类,像这样

@PersistenceCapable 
public class Property implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
        //...
}

现在,我还有另外两个课程。让我们称他们为工厂和商店。 它们都有属性,如下所示:

@PersistenceCapable 
public class Factory implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
    @Persistent
    private String m_Name;
    @Persistent
    private List<Property> m_FactoryProperties;
}

@PersistenceCapable 
public class Store implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
    @Persistent
    private String m_Name;
    @Persistent
    private List<Property> m_StoreProperties;
}

虽然这看起来很简单,但它不会起作用。 而且我不知道为什么,我猜测它与索引相关,因为在 GAE 的数据查看器中,当我查看属性实体时,要么有 m_FactoryProperties_INTEGER_IDX 要么有 m_StoreProperties_INTEGER_IDX。 (它们从不会同时出现)。

(如果我将列表重命名为 m_Propertys 也不起作用)...

这不可能吗? 我可以创建一个 FactoryProperty 和 StoreProperty 类,但这会让人感觉很糟糕。 我正在考虑的另一个解决方案是创建一个包含属性列表的超类,然后让 Factory 和 Store 从这个类继承,应该让它们共享索引,我认为...... 我应该跳过拥有的关系并选择无主的关系吗?

我觉得我可能只是错过了一些重要的东西。 有什么想法吗?

谢谢

【问题讨论】:

    标签: java google-app-engine jdo datastore


    【解决方案1】:

    在拥有关系中,属性只能属于一个类。 您还应该在父类中添加指向该属性的链接,然后

    例如。

    财产

    @Persistent
    private Store store;
    

    商店

    @Persistent(mappedBy = "store")
    private List<Property> protperties;
    

    如果你想在 store 和 factory 类中使​​用相同的属性,你必须使用 unowned 关系解决方案。

    工厂

    @Persistent
    private Set<Key> properties;
    

    商店

    @Persistent
    private Set<Key> properties;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多