简体   繁体   English

Google Datastore - 更新实体时出现问题

[英]Google Datastore - Problems updating entity

I am dusting off my google app-engine / datastore skills... and getting stuck on something very simple.我正在清理我的谷歌应用程序引擎/数据存储技能......并陷入非常简单的事情。

As per the example on the GAE documentation I am trying to update an entity as follows:根据GAE 文档中的示例,我正在尝试按如下方式更新实体:

// persistence and business logic
PersistenceManager pm = PMF.get().getPersistenceManager();

// get it
NickName n = pm.getObjectById(NickName.class, nicknameId);

// update fields
n.givenName = "new name";
n.nickName = "new nickname";
n.timeStamp = new Date();               

// close manager to persist changes
pm.close();

This doesn't work (as in the changes are not persisted, but no errors or anything else)!这不起作用(因为更改没有持久化,但没有错误或其他任何东西)!

At the same time I found that if I create a new entity with the same ID the changes get persisted:同时我发现,如果我创建一个具有相同 ID 的新实体,更改会保持不变:

// persistence and business logic
PersistenceManager pm = PMF.get().getPersistenceManager();

NickName n = new NickName("new name", "new nickname", new Date());

// set id
n.id = nicknameId;

pm.makePersistent(n);

pm.close();

I have the feeling I already solved this the 1st time I approached app engine and the data-store.我觉得我第一次接触应用程序引擎和数据存储时已经解决了这个问题。

This is what my entity looks like:这是我的实体的样子:

@PersistenceCapable
public class NickName {

    public NickName(String name, String nickname, Date timestamp) {
        this.givenName = name;
        this.nickName = nickname;
        this.timeStamp = timestamp;
    }

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public String id;

    @Persistent
    public String givenName;

    @Persistent
    public String nickName;

    @Persistent
    public Date timeStamp;
}

Any help appreciated!任何帮助表示赞赏!

One issue may be that you are setting the fields directly instead of going through the setter methods.一个问题可能是您直接设置字段,而不是通过 setter 方法。 I'm fairly certain that JDO works by instrumenting the field setters so that they notify the persistence layer of any changes that occur.我相当肯定 JDO 通过检测字段设置器来工作,以便它们通知持久层发生的任何更改。 It has no way of directly monitoring changes to the backing field values themselves.它无法直接监控对支持字段值本身的更改。 So maybe try:所以也许尝试:

n.setGivenName("new name");
n.setNickName("new nickname");
n.setTimeStamp(new Date()); 

You're able to get away with setting the field directly when you create the object because the makePersistent() call tells the persistence manager that is needs to inspect the field values and save them.您可以在创建 object 时直接设置字段,因为makePersistent()调用告诉持久性管理器需要检查字段值并保存它们。 Though it's worth noting that setting field values directly like this is generally considered to be poor coding style.虽然值得注意的是,像这样直接设置字段值通常被认为是糟糕的编码风格。

Also, have you tried using the JPA interface instead of the JDO interface?另外,您是否尝试过使用 JPA 接口而不是 JDO 接口? In GAE they should be interchangeable:在 GAE 中,它们应该是可互换的:

EntityManager em = EMF.get();

NickName n = em.find(NickName.class, nicknameId);

n.givenName = "new name";
n.nickName = "new nickname";
n.timeStamp = new Date();    

em.merge(n);

em.close();

This gives you an explicit merge() call which should work even with setting the field values directly.这为您提供了一个明确的merge()调用,即使直接设置字段值也应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新Google数据存储区中的现有数据 - Updating existing data in google datastore 将 Json 转换为 Google 数据存储实体 - Convert Json to Google Datastore Entity 使用JDO在Google数据存储区中创建实体时出错 - Error creating an entity in Google Datastore with JDO Google App Engine 数据存储实体未被删除 - Google App Engine datastore Entity not being deleted 如何 map 将 Google 云数据存储实体 (com.google.cloud.datastore.Entity) 提取到自定义 Java object - How to map fetched Google cloud datastore Entity (com.google.cloud.datastore.Entity) to Custom Java object Google 数据存储区实体不返回 ID。 实体 ID 为 null - Google Datastore Entity does not return ID. Entity ID is null 如何从 com.google.datastore.v1.Entity 获取 Datastore 实体 ID - How to get Datastore entity id from com.google.datastore.v1.Entity 使用Google Cloud Dataflow删除或更新数据存储属性 - Deleting or updating Datastore properties using Google Cloud Dataflow 使用Google App Engine数据存储区添加实体时的行为不一致 - Inconsistent behavior when adding an Entity using Google App Engine datastore 从Google数据存储区加载DDD实体的设计模式是什么? - What are design patterns to load DDD entity from Google Datastore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM