在谷歌应用程序引擎数据存储更新查询(Java) [英] Update query in google app engine data store (java)

查看:85
本文介绍了在谷歌应用程序引擎数据存储更新查询(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用gwt时在谷歌应用引擎中使用更新查询。我试图创建一个聊天应用程序,除了提交和删除以前的消息之外,管理员可以编辑现有消息的各个部分。



编辑现有消息更新查询是必要的,我找不到像数据存储中的更新查询。



我们如何更新现有数据?

以下是一些来自 http://www.ibm.com/developerworks/java/library/j-gaej3.html
你可以做一个get修改你的数据然后make一个持久化然后提交。


查看附加代码中的updateContact()方法。



注意:DataStore中的数据存储与关系数据库不同。

  package gaej.example.contact.server; 

导入gaej.example.contact.client.Contact;

import java.util.List;

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

public class ContactJdoDAO实现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();
尝试{
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();

//我们没有对选定产品的参考。
//所以我们必须先查找它,
contact = pm.getObjectById(Contact.class,contact.getId());
pm.deletePersistent(contact);

pm.currentTransaction()。commit();
} catch(Exception ex){
pm.currentTransaction()。rollback();
抛出新的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();

尝试{
pm.currentTransaction()。begin();
//我们没有对所选产品的引用。
//所以我们必须先查看它,
contact = pm.getObjectById(Contact.class,contact.getId());
contact.setName(name);
contact.setPhone(电话);
contact.setEmail(email);
pm.makePersistent(contact);
pm.currentTransaction()。commit();
} catch(Exception ex){
pm.currentTransaction()。rollback();
抛出新的RuntimeException(ex);
} finally {
pm.close();
}
}

}


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?

解决方案

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();
        }
    }

}

这篇关于在谷歌应用程序引擎数据存储更新查询(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆