更新谷歌应用程序引擎数据存储中的查询(java)

发布于 2024-08-29 10:54:58 字数 157 浏览 6 评论 0原文

如何在与 gwt 一起使用时在 Google 应用程序引擎中使用更新查询。我正在尝试制作一个聊天应用程序,除了提交和删除以前的消息之外,管理员还可以编辑现有消息的部分。

为了编辑现有消息,需要更新查询,但我在数据存储中找不到类似更新查询的内容。

我们如何更新现有数据?

How to use the update query in google app engine while using with gwt. I'm trying to make a chat application where apart from submitting and deleting the previous messages, the administrator can edit out the portions of existing messages.

For editing the existing messages update query is needed and I could not find anything like update query in data store.

How can we update the existing data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-09-05 10:54:58

以下是 http://www.ibm.com/ 的一些示例代码developerworks/java/library/j-gaej3.html
您可以先获取修改数据,然后进行持久化,然后提交。

请参阅随附代码中的 updateContact() 方法。

主要需要注意的是跨实体执行此操作 - 注意:DataStore 中的数据存储与关系数据库不同。

package gaej.example.contact.server;

import gaej.example.contact.client.Contact;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

public class ContactJdoDAO implements ContactDAO {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    public static PersistenceManagerFactory getPersistenceManagerFactory() {
        return pmfInstance;
    }

    public void addContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        try {
            pm.makePersistent(contact);
        } finally {
            pm.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<Contact> listContacts() {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        String query = "select from " + Contact.class.getName();
        return (List<Contact>) pm.newQuery(query).execute();
    }

    public void removeContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        try {
            pm.currentTransaction().begin();

            // We don't have a reference to the selected Product.
            // So we have to look it up first,
            contact = pm.getObjectById(Contact.class, contact.getId());
            pm.deletePersistent(contact);

            pm.currentTransaction().commit();
        } catch (Exception ex) {
            pm.currentTransaction().rollback();
            throw new RuntimeException(ex);
        } finally {
            pm.close();
        }
    }

    public void updateContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        String name = contact.getName();
        String phone = contact.getPhone();
        String email = contact.getEmail();

        try {
            pm.currentTransaction().begin();
            // We don't have a reference to the selected Product.
            // So we have to look it up first,
            contact = pm.getObjectById(Contact.class, contact.getId());
            contact.setName(name);
            contact.setPhone(phone);
            contact.setEmail(email);
            pm.makePersistent(contact);
            pm.currentTransaction().commit();
        } catch (Exception ex) {
            pm.currentTransaction().rollback();
            throw new RuntimeException(ex);
        } finally {
            pm.close();
        }
    }

}

Here is some sample code from http://www.ibm.com/developerworks/java/library/j-gaej3.html
You can do a get modify your data then a make persistent and then commit.

See the updateContact() method in the attached code.

The main caveat is doing this across entities - Note: Data storage in the DataStore is different than a relational DB.

package gaej.example.contact.server;

import gaej.example.contact.client.Contact;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

public class ContactJdoDAO implements ContactDAO {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    public static PersistenceManagerFactory getPersistenceManagerFactory() {
        return pmfInstance;
    }

    public void addContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        try {
            pm.makePersistent(contact);
        } finally {
            pm.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<Contact> listContacts() {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        String query = "select from " + Contact.class.getName();
        return (List<Contact>) pm.newQuery(query).execute();
    }

    public void removeContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        try {
            pm.currentTransaction().begin();

            // We don't have a reference to the selected Product.
            // So we have to look it up first,
            contact = pm.getObjectById(Contact.class, contact.getId());
            pm.deletePersistent(contact);

            pm.currentTransaction().commit();
        } catch (Exception ex) {
            pm.currentTransaction().rollback();
            throw new RuntimeException(ex);
        } finally {
            pm.close();
        }
    }

    public void updateContact(Contact contact) {
        PersistenceManager pm = getPersistenceManagerFactory()
                .getPersistenceManager();
        String name = contact.getName();
        String phone = contact.getPhone();
        String email = contact.getEmail();

        try {
            pm.currentTransaction().begin();
            // We don't have a reference to the selected Product.
            // So we have to look it up first,
            contact = pm.getObjectById(Contact.class, contact.getId());
            contact.setName(name);
            contact.setPhone(phone);
            contact.setEmail(email);
            pm.makePersistent(contact);
            pm.currentTransaction().commit();
        } catch (Exception ex) {
            pm.currentTransaction().rollback();
            throw new RuntimeException(ex);
        } finally {
            pm.close();
        }
    }

}
一曲爱恨情仇 2024-09-05 10:54:58

对已检索或先前插入的实体调用 makePersistent() 将更新数据存储中的实体。请参阅文档

Calling makePersistent() on an entity that has been retrieved or previously inserted will update the entity in the datastore. See the docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文