GAE JDO 查询结果为空

发布于 2024-12-02 23:23:30 字数 1986 浏览 1 评论 0原文

我正在组装一个简单的 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 技术交流群。

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

发布评论

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

评论(1

很酷又爱笑 2024-12-09 23:23:30

问题可能在于您处理 PersistenceManager 的方式。如下定义一个类 PMF.java:

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    private PMF() {
    }

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

然后,当您想要访问 PersistenceManager 时,您应该调用:

PersistenceManager pm = PMF.get().getPersistenceManager();

此外,您的所有模型(示例中的 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:

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    private PMF() {
    }

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

Then, when you want to get access to the PersistenceManager, you should call:

PersistenceManager pm = PMF.get().getPersistenceManager();

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!

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