JDO GAE一对多问题

时间:2011-03-21 15:05:52

标签: google-app-engine google-cloud-datastore jdo

您好我在从数据存储区中检索数据时遇到了一些问题...

我有几个马戏团,他们有很多吸引力。

马戏团:

@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class Circus implements Serializable
{
    @PrimaryKey  
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)  
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")  
    private String key;
    @Persistent
    private String name;
    @Persistent(mappedBy = "owningCircus")
    @Element(dependent = "true")
    public List<Attractions> attractions;

    public Circus()
    {

    }

    public Circus(String name)
    {
        this.name = name;
        this.attractions = new ArrayList<Attractions>();
    }
    public void addAttraction(Attraction attr)
    {
            this.attractions.add(attr);
    }
    // Get/Set
}

景点:

@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class Attraction implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
    @Persistent
    private String name;
    @Persistent 
    private Circus owningCircus;

    //...

    public Attraction()
    {

    }

    public Attraction(Circus owning, String name)
    {
        this.name = name;
        this.owningCircus = owning;
    }
    // Get/Set
}

这就是我得到马戏团的方式

public List<Circus> getCircuses()
    {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery("select from " + Circus.class.getName()); 
        List<Circus> circuses = (List<Circus>) query.execute();

        for(Circus c : circuses)
        {
            //Poke the list of attractions so they are loaded.
            //however, c.attractions==null => NullPointerException
            c.attractions.size();
        } 

        List<Circus> ret = (List<Circus>) pm.detachCopyAll(circuses);       

        pm.close();

        return ret;
    }

马戏团像这样坚持

public void addCircus(Circus circus)
    {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.makePersistent(circus);
        pm.close();
    }
//...
Circus a =new Circus("Super Circus");
a.addAttraction(new Attraction(a,"George the clown"));
addCircus(a);

c.attractions由于某种原因是空的.. 如果我查看数据查看器,有马戏团和景点..:s

由于

修改 添加了马戏团如何持续存在,以及拥有赛马会。

编辑2

我实际上得到了这个例外:

com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found..      <datastore-index kind="Attraction" ancestor="true" source="manual">
        <property name="attractions_INTEGER_IDX" direction="asc"/>

2 个答案:

答案 0 :(得分:1)

添加包含

的datastore-indexes.xml
<?xml version="1.0" encoding="utf-8"?> <datastore-indexes   autoGenerate="true"> 
<datastore-index kind="Attraction" ancestor="true" source="manual">
        <property name="attractions_INTEGER_IDX" direction="asc"/>
    </datastore-index>
</datastore-indexes>

to war / WEB-INF /解决了这个问题。

答案 1 :(得分:0)

在JDO over GAE中,键入的引用意味着拥有的关系。因此,为了使其工作,请尝试添加

@Persistent(mappedBy="owningCircus")

马戏团吸引力添加字段

@Persistent private Circus owningCircus;

这种方式在创建新景点时,设置马戏团将隐式映射关系。 (参见下面链接中的“实体组”)

如果您不想拥有自己的关系,只需按住键列表即可。 有关详情,请参阅http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html