java - GAE 与 JPA : Update entity

标签 java google-app-engine jpa

我在更新 Google App Engine 中的实体时遇到问题。

EntityManager em = ... // constructed like in the doc

MyEntity myE = new MyEntity();
myE.setType("1");      // String
em.persist(myE);em.refresh(myE);

myE.setType("2");
em.merge(myE);em.refresh(myE);

我期望一个具有 type="2" 的实体,但只有一个具有 type="1" 的实体:-(

最佳答案

这是正确的行为,让我解释一下(我假设您的所有代码都在相同的持久性上下文/事务中运行)。

# This line sets the value in the in-memory object without changing the database
myE.setType("2");

# this line doesn't do anything here, as the entity is already managed in the current 
# persistence context. The important thing to note is that merge() doesn't save the 
# entity to the DB.
em.merge(myE);

# This reloads the entity from the DB discarding all the in-memory changes.
em.refresh(myE);

关于java - GAE 与 JPA : Update entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7398119/

相关文章:

google-app-engine - gcloud preview 应用程序部署过程需要大约 8 分钟,这正常吗?

java - Hibernate可以在没有Spring的情况下生成表吗?

java - "this"在例如 Intent(this, ClassName.class) 中使用时如何实际提供来自 Activity 的上下文

java - Ajax 后 Spring MVC Controller ?

php - Google Cloud Storage 一个文件同时访问多个

eclipse - 如何将 Web 应用程序部署到 Google App Engine

java - 如何使用 primefaces 将 View 中的值发送到我的 bean?

java - 使用 Jackson 响应 json Web 服务

Java 标准构建器查询不为空或为空

hibernate - 如何修复此 "no persistent classes found for query class"消息?