【问题标题】:How to fetch Entities, update in GWT, and save using JPA 2 in AppEngine?如何在 AppEngine 中使用 JPA 2 获取实体、在 GWT 中更新和保存?
【发布时间】:2013-10-25 01:10:56
【问题描述】:

在我更新 GWT 中的一些实体后,我想保存它们。但是,当我尝试保留它们时,当我查看 AppEngine 管理界面时它不会保存。布尔值没有改变。

代码

    EntityManager em = EMF.get().createEntityManager();
    for (OnixUser s: admin) {
        log.info(s.email + ", " + s.isAdmin);
        em.merge(s);  
    }
    em.close();

交易更新

    EntityManager em = EMF.get().createEntityManager();
    em.getTransaction().begin();
    for (OnixUser s: admin) {
        log.info(s.email + ", " + s.isAdmin);
        OnixUser merged = em.merge(s);
        em.persist(merged);
//          em.persist(s);
    }
    em.getTransaction().commit();
    em.close();

仍然没有保存。没有抛出异常。

日志

 Oct 16, 2013 3:19:10 PM com.example.sdm.server.SDMServiceImpl setAdmin
 INFO: chloe@example.com, true

OnixUser 实体的 App Engine 管理界面

以 FINEST 级别登录

FINE: Created ManagedConnection using DatastoreService = com.google.appengine.api.datastore.DatastoreServiceImpl@2fd9270d
Oct 16, 2013 4:03:14 PM org.datanucleus.store.connection.ConnectionManagerImpl allocateConnection
FINE: Connection added to the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@31c1f89d for key=org.datanucleus.ObjectManagerImpl@6977c57b in factory=ConnectionFactory:tx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@2b1f5f6b]
Oct 16, 2013 4:03:14 PM com.example.sdm.server.SDMServiceImpl setAdmin
INFO: chloe@example.com, true
Oct 16, 2013 4:03:14 PM org.datanucleus.state.LifeCycleState changeState
FINE: Object "com.example.sdm.shared.OnixUser@48ef6e99" (id="com.example.sdm.shared.OnixUser:6456332278300672") has a lifecycle change : "P_CLEAN"->"P_NONTRANS"
Oct 16, 2013 4:03:14 PM org.datanucleus.store.connection.ConnectionManagerImpl$1 managedConnectionPostClose
FINE: Connection removed from the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@31c1f89d for key=org.datanucleus.ObjectManagerImpl@6977c57b in factory=ConnectionFactory:tx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@2b1f5f6b]
Oct 16, 2013 4:03:14 PM org.datanucleus.state.LifeCycleState changeState
FINE: Object "com.example.sdm.shared.OnixUser@48ef6e99" (id="com.example.sdm.shared.OnixUser:6456332278300672") has a lifecycle change : "P_NONTRANS"->"DETACHED_CLEAN"
Oct 16, 2013 4:03:14 PM com.google.apphosting.utils.jetty.AppEngineAuthentication$AppEngineUserRealm disassociate
FINE: Ignoring disassociate call for: chloe@example.com

【问题讨论】:

  • 我没有使用过 GWT,但是您的代码没有使用 em.getTransaction().begin() 和 em.getTransaction().commit() 启动/提交事务。如果没有事务,实体管理器不会按照 JPA 规范对数据库进行任何更改。
  • @Shailendra 我认为这是可选的,但我添加了事务,通过管理界面查看或重新加载页面时仍然没有保存。
  • 您不需要调用来持久化,因为“合并”指向的对象将被管理,并且任何进一步的更改都将在事务提交时自动持久化。您可以启用 sql 日志记录以查看该操作是否确实生成了任何 SQL。
  • @Shailendra 我尝试使用和不使用 .persist(),并在两者之间重新启动服务器。如何打开 SQL 日志记录?
  • 您为 JPA 使用哪个持久性提供程序?

标签: java google-app-engine gwt jpa datanucleus


【解决方案1】:

如果尽管使用了事务,但您的数据没有持久化,那么最好的方法是尝试记录以查看下面发生的情况。当你使用 DataNucleus 作为你的持久化提供者时,你可以参考这个link 来配置 SQL 日志记录。与您相关的信息在页面末尾附近提供。

【讨论】:

  • 好的,我修改了WEB-INF/logging.properties并重新启动了服务器,但它没有打印任何附加信息:pastie.org/8407353
  • 好的,我必须停止并启动服务器,而不仅仅是重新加载。使用 .level=FINEST DataNucleus.level=FINEST 并导致此日志:pastie.org/8407382
  • 能否贴出相关的persistence.xml。我想看的是事务类型是JTA还是RESOURCE_LOCAL。如果是前者,那么您可能需要注入实体管理器而不是自己创建。
  • 用户正在使用 GAE。没有这样的 SQL。显然,用于 JPA 的 GAE 插件确实会在 DEBUG 级别打印出很多日志信息,包括所有 PUT 到数据存储区。
  • 那看来我是误会环境了。
猜你喜欢
  • 2012-01-03
  • 2018-04-20
  • 2012-01-05
  • 2013-05-10
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
相关资源
最近更新 更多