GAE JDO空查询结果

时间:2011-09-03 19:28:30

标签: google-app-engine jdo rpc

我正在整理一个简单的GWT,GAE,JDO应用程序。首先,我试图存储一张Cat唱片表。我设法让RPC机制在我的调用中工作以保存数据(参见下面的setCOTDCats())。据我所知,RPC调用是成功的。但是,当我尝试重新读取值时(请参阅下面的getCOTDCats()),它似乎也可以工作,除了它返回一个空结果。

显然,保存不起作用,或者读取没有,但我不知道如何判断哪个不起作用。

我是GAE和JDO新手。任何回复都请考虑到这一点。

由于

没有Snark请

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

1 个答案:

答案 0 :(得分:0)

问题可能在于您处理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();

此外,您应使用JDO定义所有模型(示例中的Cat)。有关详细信息,请访问:http://code.google.com/appengine/docs/java/datastore/jdo/

希望这有帮助!