JDO GAE 一对多问题

发布于 2024-10-24 00:23:38 字数 2898 浏览 5 评论 0原文

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

我有一些马戏团,他们有一些吸引力。

马戏团:

@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 由于某种原因为空。 如果我查看数据查看器,就会看到马戏团和景点..:

谢谢

编辑: 添加了如何保留马戏团以及拥有马戏团。

编辑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"/>

Hi i am having some problems retreiving data from the datastore...

I have a few circuses, who has some amount of attractions.

Circus:

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

Attraction:

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

This is how i get the circuses

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

Circus is persisted like this

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 is null for some reason..
If i look into the Data Viewer there are both circuses and attractions.. :s

Thanks

EDIT:
Added how the circus is persisted, aswell as owningCircus.

EDIT 2

I am actually getting this exception:

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"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国粹 2024-10-31 00:23:38

添加包含到 war/WEB-INF/ 的 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>

解决了这个问题。

Adding a datastore-indexes.xml containing

<?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/ solved the issue.

完美的未来在梦里 2024-10-31 00:23:38

在 GAE 上的 JDO 中,类型化引用意味着拥有关系。因此,要使其正常工作,请尝试添加

@Persistent(mappedBy="owningCircus")

CircusAttraction添加字段,

@Persistent private Circus owningCircus;

这样在创建新景点时,设置Circus将隐式映射关系。 (请参阅下面链接中的“实体组”)

如果您不想要拥有关系,只需持有键列表即可。
有关详细信息,请访问 http://code.google.com/appengine /docs/java/datastore/jdo/relationships.html

In JDO over GAE, typed references imply an owned relationship. So to make it work try adding

@Persistent(mappedBy="owningCircus")

to Circus and to Attraction add the field

@Persistent private Circus owningCircus;

This way when creating a new attraction, setting the Circus will implicitly map the relationship. (see "entity groups" in the link below)

If you do not want an owned relationship, simply hold a list of Keys.
More on this at http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文