-4

如何在 AppEngine 中为一个全新的、空的、不存在的表加载字符串列表?我试着按照这个例子:

http://www.objectdb.com/java/jpa/query/jpql/select#Projection_of_Path_Expressions_

但它给了我一个错误:

原因:javax.persistence.PersistenceException:用于查询的类 AdminUser 尚未解决。检查查询和任何导入/别名规范

原因:org.datanucleus.exceptions.ClassNotResolvedException:用于查询的类 AdminUser 尚未解决。检查查询和任何导入/别名规范

这是代码:

public java.util.List<String> getAdmin() {
    EntityManager em = EMF.get().createEntityManager();
    try {
        TypedQuery<String> tq = em.createQuery("select au.email from AdminUser as au", String.class);
        return tq.getResultList();  ///// <=== EXCEPTION

我实际上不想使用 AdminUser 类。我只想要一列字符串。如何在 AppEngine 上创建一个新的空表并不明显。

4

2 回答 2

1

GAE Datastore 是一个无模式 NoSQL 数据库。没有桌子。只有实体必须具有种类、id 并且可以具有任意一组属性。

您可以通过 JPA API 使用 Datastore,为您提供漂亮的类型化 Java 类,而不是低级的无类型实体

于 2013-10-10T18:37:15.370 回答
-1

AppEngine 不支持示例链接中的 JPA 原始原始查询。我必须直接使用 DatastoreService API。这是我尝试过的。需要一些清洁,因为很多东西都不起作用,但这确实有效。

    public java.util.List<String> getAdmin() {
        log.info("getAdmin()");

        // AppEngine does not support strong consistency... will frequently return stale results. Setting read policy won't work.
        // Construct a read policy for strong consistency
        ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG);
        // Set the read policy
        DatastoreServiceConfig consistentConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy);
        // Get Datastore service with the given configuration
        DatastoreService datastoreService =  DatastoreServiceFactory.getDatastoreService(consistentConfig);

        com.google.appengine.api.datastore.Query query  = new com.google.appengine.api.datastore.Query("AdminUser");
//      DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
        java.util.List<Entity> events = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
        ArrayList<String> al = new ArrayList<String>();
        for(Entity entity: events) {
            String s = entity.getProperty("email").toString();
            al.add(s);
        }
        if(true) return al; 


        @SuppressWarnings("unused")
        EntityManager em = EMF.get().createEntityManager();
        try {
            TypedQuery<String> tq = em.createQuery("select au.email from AdminUser as au", String.class);
            return tq.getResultList();
        } catch (ClassNotResolvedException | javax.persistence.PersistenceException cnre) {
            // catch the exception because AppEngine DataStore has no way to create a new empty Entity table.
            log.warning("AdminUser entity does not exist or is empty.");
            return new java.util.ArrayList<String>();
        } finally {
            em.close(); 
        }

//      log.info("getAdmin(): al.size(): " + al.size());
    }


    public void setAdmin(java.util.List<String> admin) {
        log.info("setAdmin(), admin.size():"+admin.size());

        // delete all AdminUsers
        com.google.appengine.api.datastore.Query query  = new com.google.appengine.api.datastore.Query("AdminUser");
        DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
        java.util.List<Entity> events = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
        for(Entity entity: events) {
            datastoreService.delete(entity.getKey());
        }


//      EntityManager em = EMF.get().createEntityManager();
//      Query q = em.createQuery("delete from AdminUser");
//      q.executeUpdate();


//      DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
        for (String s: admin) {
            log.info("creating AdminUser Entity...");
            Entity e = new Entity("AdminUser");
            e.setProperty("email", s);
            datastoreService.put(e);    
        }
        // Must wait for Datastore to actually write records. Ignores consistency even with strong consistency set.
        // this doesn't even work sometimes...
        //try { Thread.sleep(5000); } catch (InterruptedException e) {}

//      EntityManager em = EMF.get().createEntityManager();
//      for (String s: admin) {
//          Entity e = new Entity("AdminUser");
//          e.setProperty("email", s);
//          em.persist(e);
//      }
//      em.close();

    } // setAdmin()
于 2013-10-11T15:57:17.343 回答