Google App Engine JDO问题

时间:2010-07-20 09:11:51

标签: java google-app-engine jdo

如何存储复杂的物体?我在子对象的子对象列表中有一个对象,在调用makePersistent(person)对象时没有保存四个子对象列表。帮助!!!!!!!!!!!!

我打电话给pm.makePersistent();但列表

@Persistent
    private List<ChoosedElementEntity> choosedElements = new ArrayList<ChoosedElementEntity>();

    @Persistent
    private List<PleasantElementEntity> pleasantElements = new ArrayList<PleasantElementEntity>();

    @Persistent
    private List<UnpleasantElementEntity> unpleasantElements = new ArrayList<UnpleasantElementEntity>();

    @Persistent
    private List<SetViewElementEntity> setViewElements = new ArrayList<SetViewElementEntity>();

没有保存!!!

示例:

@PersistenceCapable(table = "persons", identityType = IdentityType.APPLICATION)
public class PersonEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;   

    @Persistent
    private List<OfferEntity> offers = new ArrayList<OfferEntity>();
}

@PersistenceCapable(table = "offers", identityType = IdentityType.APPLICATION)
public class OfferEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;


    @Persistent
    private List<ChoosedElementEntity> choosedElements = new ArrayList<ChoosedElementEntity>();

    @Persistent
    private List<PleasantElementEntity> pleasantElements = new ArrayList<PleasantElementEntity>();

    @Persistent
    private List<UnpleasantElementEntity> unpleasantElements = new ArrayList<UnpleasantElementEntity>();

    @Persistent
    private List<SetViewElementEntity> setViewElements = new ArrayList<SetViewElementEntity>();
}


@PersistenceCapable(table = "offer_selections", identityType = IdentityType.APPLICATION)
public class ChoosedElementEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}

@PersistenceCapable(table = "offer_selections", identityType = IdentityType.APPLICATION)
public class PleasantElementEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}

@PersistenceCapable(table = "offer_selections", identityType = IdentityType.APPLICATION)
public class UnpleasantElementEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}

3 个答案:

答案 0 :(得分:2)

  • 检查您是否已将对象层次结构中的所有类声明为@PersistenceCapable
  • 检查您是否按照google documentation声明了列表/设置对象变量?即:
    @Element(dependent = "true") 
    private Set tags = new HashSet(); 
    
  • 您是否尝试过使用makePersistentAll(person)
  • 检查您的数据是否真的被保存,并在http://localhost:8888/_ah/admin/
  • 查看您的数据
  • 您是否正在关闭Persistence处理程序?即pm.close()。在关闭处理程序之前,数据永远不会持久化,即:
    // All objects are manupliated using a pm object
    PersitenceManager pm = PMF.instance().getPersistenceManager();
    // do work
    // Store the changes
    pm.close()
    

答案 1 :(得分:0)

这可能是对象之间关系的问题? 尝试手动保存复杂对象中的每个对象

答案 2 :(得分:0)

您似乎没有在JDO中正确激活生成器主键。这不是JDO的错误吗?

我找到了它:

“目前存在一个阻止拥有一对多关系的错误 父母和孩子是同一个班级,使其变得困难 模拟树结构。这将在以后的版本中修复。您 可以通过存储显式键值来解决这个问题 父母或子女。“

http://code.google.com/intl/en-EN/appengine/docs/java/datastore/usingjdo.html

我等不及修复了,所以我已经解决了这个问题 通过拥有这种类型的所有实体来解决问题:

  • 具有相同的实体组父(不同类)
  • 使用ArrayList&lt; [Entity]&gt;存储每个实体的“孩子”的关键值
  • 使用键值存储每个实体的“父”(对于根实例为null,即使它仍然具有实体组父实例)。