我在更新 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/