簡體   English   中英

在谷歌應用引擎數據存儲中更新查詢(java)

[英]Update query in google app engine data store (java)

如何在使用gwt時在Google App引擎中使用更新查詢。 我正在嘗試創建一個聊天應用程序,除了提交和刪除以前的消息之外,管理員還可以編輯現有消息的部分內容。

要編輯現有消息,需要更新查詢,我在數據存儲中找不到類似更新查詢的內容。

我們如何更新現有數據?

以下是來自http://www.ibm.com/developerworks/java/library/j-gaej3.html的一些示例代碼您可以執行修改數據然后執行make persistent然后提交。

請參閱附加代碼中的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();
        }
    }

}

在已檢索或先前插入的實體上調用makePersistent()將更新數據存儲區中的實體。 查看文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM