Google app engine 为什么';t EntityManager.flush()是否在JPA1.0/AppEngine中工作?
当我有一个名为“em”的对象并调用Google app engine 为什么';t EntityManager.flush()是否在JPA1.0/AppEngine中工作?,google-app-engine,jpa,datanucleus,Google App Engine,Jpa,Datanucleus,当我有一个名为“em”的对象并调用em.flush()时,什么都不会发生。我希望当前事务-即此时写入数据库的数据。为什么没有发生 这是我工作的商务舱: @Entity public class SomeObject { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long Id; public String Property1; public String Property2
em.flush()
时,什么都不会发生。我希望当前事务-即此时写入数据库的数据。为什么没有发生
这是我工作的商务舱:
@Entity
public class SomeObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long Id;
public String Property1;
public String Property2;
}
这是写入和刷新的测试代码:
public void transactionTest() {
EntityManager em = Database.getEntityManager();
em.getTransaction().begin();
SomeObject obj = new SomeObject();
obj.Id = 2L;
obj.Property1 = "A";
em.persist(obj);
em.flush();
obj.Property2 = "B"; // Breakpoint here, expecting data to have be written
em.getTransaction().commit(); // ...but the data is not written until after this line.
em.close();
}
这是我的persistence.xml
<persistence>
<persistence-unit name="no-xtransactions">
<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
<properties>
<property name="datanucleus.nontx.atomic" value="false"/>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="datanucleus.appengine.datastoreEnableXGTransactions" value="false"/>
</properties>
</persistence-unit>
</persistence>
org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider
我正在使用JPA/JDO 1.0在AppEngine SDK v1.7.4的本地数据存储上测试它,方法是在带有注释的行上设置断点,同时检查AppEngine API生成的localhost web界面中的数据。在我关闭EntityManager之前,不会存储该对象
我还通过同时实例化两个EntityManager在生产AppEngine上对此进行了测试,以便第二个EntityManager在em.flush()
-行之后进行读取。不过,我不确定测试方法是否良好(您可以有两个EntityManager吗?),因此我不确定是否可以信任结果。您使用的是旧的(不受支持的)代码(即JPA1 GAE)。
您正在更新实体的公共字段(根据JPA规范无效,无论如何都是错误的做法)
解决方案很简单:使用JPA2 GAE(大约2年前发布),不要更新实体的公共字段(使用setter,或者使用DataNucleus的“PersistenceAware”扩展)带有更新代码的类被标记为@PersistenceAware,所以公共字段是有效的。转换成getter/setters和JPA2可能需要几周的时间,所以我不会说这是一个“简单的解决方案”,但我猜这是唯一的解决方案。