java - Google App Engine 数据存储实体未被删除

标签 java google-app-engine objectify

我正在使用 Google App Engine 数据存储来存储 4 个字符串值。字符串值被添加到 servlet 中的数据存储区:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Entity balances;
        Key primaryKey;
        String table = "MainTable";
        String name = "Values";

        primaryKey = KeyFactory.createKey(table, name);

        Transaction t = datastore.beginTransaction();
            // If the 'table' exists - delete it
        datastore.delete(primaryKey);
            // Really make sure it's deleted/
        t.commit();

        t = datastore.beginTransaction();

            balances = new Entity("Balances", primaryKey);
        updateBalances(balances);
        datastore.put(balances);

            // Save the new data
        t.commit();
        resp.sendRedirect("/balance.jsp");

我希望能够在每次运行 servlet 时更新四个字符串值 - 这就是我首先查找键并将其删除的原因。我什至使用一个单独的事务来确保这真的发生了。

找到并删除键,然后添加值。但是,当我加载一个检索值的 .jsp 文件时,实体中的“记录”数量每次都会增加 1。我不明白为什么记录没有被删除。

这是 .jsp 代码:

  <%
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Key guestbookKey = KeyFactory.createKey("MainTable", "Values");

        Query query = new Query("Balances", guestbookKey);

        List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    %>
<!-- This should always be 1, but it gorws each time the servlet is hit.-->
    <%= greetings.size() %>

解决方案

我不知道原始问题中的代码有什么问题。但是,我通过使用名为 Objectify (http://code.google.com/p/objectify-appengine/) 的库实现了在 Google App Engine (GAE) 上跨 session 保留字符串值的目标 - 旨在简化使用GAE 上的 DataStore。

库本身只是一个 .jar 文件,可以很容易地添加到 Eclipse 中的 Java 项目中。我没有发现使用易于使用的库...主要问题是注册模拟您希望保存的数据的类。注册只能进行一次!

为了仅在我向我的 Web 应用程序添加一个监听器后注册该类,该应用程序使用 Objectify 框架注册了该类并创建了 4 个随机数并保存了它们:

public class MyListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {

            // Register the Account class, only once!
        ObjectifyService.register(Account.class);

        Objectify ofy = ObjectifyService.begin();
        Account balances = null;

            // Create the values we wish to persist.
        balances = new Account(randomNum(), randomNum(), randomNum(),
                randomNum());

            // Actually save the values.
        ofy.put(balances);
        assert balances.id != null;    // id was autogenerated
    }

    public void contextDestroyed(ServletContextEvent event) {
        // App Engine does not currently invoke this method.
    }

    private String randomNum() {
        // Returns random number as a String
    }
}

.. 此代码仅在服务器启动时运行一次 - 为此,我还需要修改 web.xml 以添加:

<listener>
        <listener-class>.MyListener</listener-class>
    </listener>

然后我就有了一个读取保存值的 .jsp 页面:

<%
Objectify ofy = ObjectifyService.begin();
boolean data = false;
// The value "mykey" was hard coded into my Account class enter code here 
// since I only wanted access to the same data every time.
Account a = ofy.get(Account.class, "mykey");
data = (null!=a);
%>

这是我的帐户类:

import javax.persistence.*;

public class Account
{
    @Id String id = "mykey";
    public String balance1, balance2, balance3, balance4;

    private Account() {}

    public Account(String balance1, String balance2, String balance3, String balance4)
    {
        this.balance1 = balance1;
        this.balance2 = balance2;
        this.balance3 = balance3;
        this.balance4 = balance4;
    }
}

最后一件事...我找到了 OBjectify documentation无论 Objectify 框架如何,都有助于理解 GAE Datastore

最佳答案

为了将来引用,我认为您的原始示例因这一行而失败:

balances = new Entity("Balances", primaryKey);

这实际上并没有创建具有 primaryKey 的实体,但它创建了一个以 primaryKey 作为祖先键的实体。每次存储它时,它都会获得一个自动生成的 ID。

关于java - Google App Engine 数据存储实体未被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6776758/

相关文章:

java - 如何使用 Selenium 连接到 Chromium Headless

java - 使用环境变量测试编译jar

google-app-engine - Google Datastore Transactions 乐观并发控制与否?

java - 使用 Objectify 注册多个类

google-app-engine - 使用 objectify 在实体中搜索子字符串

java - Tink 中的多个私钥/公钥对

java - 如何在不使用字符串的情况下确定输入的整数是否包含某些数字

java - 如何在gae中下载文件

python - 在 Google App Engine 上分析/优化网站的最佳方式

java - Dev App Server 不支持 App Engine 柔性环境应用程序