java - 为什么没有定义 getEntityManager()? "Cannot resolve method getEntityManager"

标签 java android google-app-engine

我正在构建一个不使用 JDO 与 google appengine 数据存储集成的 android 应用程序。

我正在尝试构建一个端点,以允许我访问我的数据存储中的数据。我正在编写的函数在下面,但我遇到了一个奇怪的问题,尽管将这段代码放入端点 java 类中......

我得到的错误是Cannot Resolve Method getEntityManager()

我在网上看到的每一个例子,都是调用这个函数。 -- 所以必须有办法让它工作,否则我一定是在做一些愚蠢的事情。

我错过了什么?我该如何解决这个问题

@Api(name = "getPostsApi", version = "v1", namespace = @ApiNamespace(ownerDomain = "endpoints.myModule.myCo.com",
        ownerName = "endpoints.myModule.myCo.com", packagePath=""))
public class GetPostsEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @SuppressWarnings({"unchecked", "unused"})
    @ApiMethod(name = "GetPostsEndpoint")
    public CollectionResponse<NewPostBean> listStuff(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {


        EntityManager mgr = null;
        Cursor cursor = null;
        List<NewPostBean> execute = null;

        try {
            mgr = getEntityManager();   // <---- Breaks on this line

            //Query query = mgr.createQuery("select from Stuff as Stuff");
//                limit =1;

            //execute = (List<NewPostBean>) query.getResultList();
            //cursor = JPACursorHelper.getCursor(execute);
            //for (NewPostBean obj : execute)
            //    ;
        //} finally {
         //   mgr.close();
        //}

        return CollectionResponse.<NewPostBean>builder().setItems(execute).setNextPageToken(cursorString).build();
    }
}

最佳答案

当我看here ,我看到他们实际上是通过创建这个类来获得实体管理器的:

public final class EMF {
    private static final EntityManagerFactory emfInstance =
        Persistence.createEntityManagerFactory("transactions-optional");

    private EMF() {}

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

然后他们调用 EMF.get() 来获取实体管理器。我只用过它并且有效。您发现什么代码会像那样突然调用“getEntityManager”?根据您发布的代码,getEntityManager 似乎根本没有定义

关于java - 为什么没有定义 getEntityManager()? "Cannot resolve method getEntityManager",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305806/

相关文章:

python - 导入错误 : No module named endpoints_proto_datastore. ndb

java - 双向映射空值保留在子级的父引用列中

java - 检查 hashmap 中是否存在 'Key'

java - 使用 Java 创建一个文本文件以供即时下载

android - 共享元素转换和 ContentTransition 延迟计时

android - 如何在android中显示每秒动态变化的当前时间

方波上的android音频失真

java - 阻止 App Engine 记录客户端文件请求

python - Google Cloud App Engine 灵活的 Python 2.7 环境错误启动新线程

java - 解压缩时 FileOutputStream 抛出 FileNotFoundException