开发者

JDO GAE One-to-Many problem

开发者 https://www.devze.com 2023-02-17 23:33 出处:网络
Hi i am having some problems retreiving data from the datastore... I have a few circuses, who has some amount of attractions.

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消