【问题标题】:Updating collection of dependent objects更新依赖对象的集合
【发布时间】:2010-07-17 08:36:07
【问题描述】:

我有一个类Event和一个依赖类Entry,它们的实例只在事件的上下文中有效。

在 JDO 中对此进行建模的最佳方法是什么? Acutally 我不想只为事件及其条目查询条目。那么我需要一个进入的钥匙吗?

我目前的解决方案是:

@PersistenceCapable
public class Event {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long id;

    @Persistent
    public List<Entry> entries = new ArrayList<Entry>();
}

@PersistenceCapable
public class Entry {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Key key;

    @Persistent
    public String name;
}

我尝试向现有事件添加一个条目,但它并没有真正持久化更改的事件:

Event e = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
    e = pm.getObjectById(Event.class, Long.parseLong(id));

    System.out.println(e.entries.size());

    Entry entry = new Entry();
    entry.name = name;

    e.entries.add(entry);

    pm.makePersistent(e);

    System.out.println(e.entries.size());
} catch (NumberFormatException nfe) {
    return null;
} finally {
    pm.close();
}
return e;

我尝试将 Entry 设为 Embedded 实体,但不允许有嵌入对象的集合。

【问题讨论】:

    标签: java google-app-engine jdo


    【解决方案1】:

    实际上更新不是问题。我没有在我的服务中正确加载事件。

    public Event loadEvent(String id) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Event event = pm.getObjectById(Event.class, Long.parseLong(id));
            // And load entries
            for (Entry entry : event.getEntries()) {
                entry.amounts.size();
            }
            return event;
        } catch (NumberFormatException e) {
            return null;
        } finally {
            pm.close();
        }
    }
    

    在注释和返回语句之间添加行后,所有条目都正确显示。

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2021-10-12
      相关资源
      最近更新 更多