2010-07-17 74 views
0

我有一个类Event和一个依赖类Entry,哪些实例只在事件上下文中有效。更新依赖对象的集合

在JDO中对此进行建模的最佳方法是什么? Acrtally我不想查询仅用于事件及其条目的条目。那么我需要在Entry上使用密钥吗?

我目前的解决办法是:

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

我试图让入门嵌入式实体,但不允许嵌入对象的集合。

回答

0

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

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(); 
    } 
} 

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