GAE JDO 查询结果为空
我正在组装一个简单的 GWT、GAE、JDO 应用程序。首先,我尝试存储一张猫记录表。我设法让 RPC 机制在我的调用中工作以保存数据(请参阅下面的 setCOTDCats())。据我所知,RPC 调用是成功的。但是,当我尝试读回这些值时(请参阅下面的 getCOTDCats()),它似乎也可以工作,只是它返回一个空结果。
显然,要么保存不起作用,要么读取不起作用,但我不知道如何判断哪个不起作用。
我是 GAE 和 JDO 新手。请在做出任何回应时考虑到这一点。
谢谢,
请不要恶作剧
@Override
public Cat[] getCOTDCats()
{
final List<Cat> catList = new ArrayList<Cat>();
final PersistenceManager pm = getPersistenceManager();
try
{
final Query q = pm.newQuery(CatRecord.class);
q.setOrdering("COTDDate");
final List<CatRecord> catRecords = (List<CatRecord>) q.execute();
for (CatRecord catRecord: catRecords)
{
final Cat cat = catRecord.getCat();
catList.add(cat);
}
}
finally
{
pm.close();
}
final Cat[] result = catList.toArray(new Cat[0]);
return result;
}
/* (non-Javadoc)
* @see org.catadoptionteam.catserve.client.AnimalDataService#setCOTDCats(org.catadoptionteam.catserve.shared.Cat[])
*/
@Override
public void setCOTDCats(Cat[] cat)
{
final List<CatRecord> catList = new ArrayList<CatRecord>();
for (Cat c: cat)
{
final CatRecord catRecord = new CatRecord(c);
catList.add(catRecord);
}
final PersistenceManager pm = getPersistenceManager();
try
{
pm.makePersistentAll(catList);
}
finally
{
pm.close();
}
}
private static PersistenceManager getPersistenceManager()
{
final PersistenceManager result = Persister.getPersistenceManager();
return result;
}
public enum Persister
{
INSTANCE;
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
public static PersistenceManager getPersistenceManager()
{
final PersistenceManager result = pmfInstance.getPersistenceManager();
return result;
}
}
I'm putting together a simple GWT, GAE, JDO application. To start with, I'm attempting to store a table of Cat Records. I managed to get the RPC mechanism to work in my call to save the data (see setCOTDCats() below). The RPC call is successful as far as I can tell. However, when I try to read the values back (see getCOTDCats() below), it too seems to work except that it returns an empty result.
Clearly either the save didn't work, or the read didn't, but I don't know how to tell which isn't working.
I'm a GAE and JDO Newbie. Please take this into account with any responses.
Thanks
No Snark Please
@Override
public Cat[] getCOTDCats()
{
final List<Cat> catList = new ArrayList<Cat>();
final PersistenceManager pm = getPersistenceManager();
try
{
final Query q = pm.newQuery(CatRecord.class);
q.setOrdering("COTDDate");
final List<CatRecord> catRecords = (List<CatRecord>) q.execute();
for (CatRecord catRecord: catRecords)
{
final Cat cat = catRecord.getCat();
catList.add(cat);
}
}
finally
{
pm.close();
}
final Cat[] result = catList.toArray(new Cat[0]);
return result;
}
/* (non-Javadoc)
* @see org.catadoptionteam.catserve.client.AnimalDataService#setCOTDCats(org.catadoptionteam.catserve.shared.Cat[])
*/
@Override
public void setCOTDCats(Cat[] cat)
{
final List<CatRecord> catList = new ArrayList<CatRecord>();
for (Cat c: cat)
{
final CatRecord catRecord = new CatRecord(c);
catList.add(catRecord);
}
final PersistenceManager pm = getPersistenceManager();
try
{
pm.makePersistentAll(catList);
}
finally
{
pm.close();
}
}
private static PersistenceManager getPersistenceManager()
{
final PersistenceManager result = Persister.getPersistenceManager();
return result;
}
public enum Persister
{
INSTANCE;
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
public static PersistenceManager getPersistenceManager()
{
final PersistenceManager result = pmfInstance.getPersistenceManager();
return result;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能在于您处理 PersistenceManager 的方式。如下定义一个类 PMF.java:
然后,当您想要访问 PersistenceManager 时,您应该调用:
此外,您的所有模型(示例中的 Cat)都应该使用 JDO 定义。有关详细信息,请访问:http://code.google.com/appengine/ docs/java/datastore/jdo/
希望这有帮助!
Probably the problem lies in the way you handle the PersistenceManager. Define a class PMF.java as follows:
Then, when you want to get access to the PersistenceManager, you should call:
In addition, all your models (Cat in your example) should be defined with JDO. For detailed information check at: http://code.google.com/appengine/docs/java/datastore/jdo/
Hope this helps!