JDO GAE 一对多问题
您好,我在从数据存储中检索数据时遇到一些问题...
我有一些马戏团,他们有一些吸引力。
马戏团:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加包含到 war/WEB-INF/ 的 datastore-indexes.xml
解决了这个问题。
Adding a datastore-indexes.xml containing
to war/WEB-INF/ solved the issue.
在 GAE 上的 JDO 中,类型化引用意味着拥有关系。因此,要使其正常工作,请尝试添加
到Circus和Attraction添加字段,
这样在创建新景点时,设置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
to Circus and to Attraction add the field
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