2014-10-10 83 views
1

我正在構建一個與Google Appengine數據存儲集成而不使用JDO的android應用程序。爲什麼getEntityManager()未定義? 「無法解析方法getEntityManager」

我正在嘗試構建一個允許我訪問數據存儲中的數據的端點。我寫功能如下,但是,我有一個奇怪的問題,儘管把這個代碼到端點的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(); 
    } 
} 

回答

1

當我看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似乎沒有任何定義

+0

我在網上學習一個教程!我想他們離開了那部分。另外,我添加了一個變量和一個函數來返回EntityManager而不是EntityManagerFactory。非常感謝 – user198923 2014-10-10 22:12:54

+0

非常歡迎:)令人驚訝的教程留下了這樣一個重要的部分:P – Patrice 2014-10-10 22:13:35

相關問題