使用guice-persist时,无法识别或保留对实体的更新

时间:2013-12-05 02:52:25

标签: guice-servlet guice-persist

我遇到了guice-persist和guice-servlet(http-request范围的jpa会话)的问题,我试图更新实体的值并保持更新,但更新永远不会持久存储到数据库。我尝试使用entityManager.flush()entityManager.getTransaction().commit()强制写入,但是当我查看日志时,即使http会话结束并且释放了jdbc连接,也似乎没有发生任何事情。 我通常希望看到hibernate发出一个sql update语句,但更新似乎永远不会注册。令我感到奇怪的是,我创建新实体没有问题,这似乎只会影响更新。

我有一个Singleton范围的servlet,它有一个注入的UserDao,它使用注入的Provider<EntityManager>

这是我的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
<persistence-unit name="db-manager">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.turms.server.database.DurableUser</class>
    <properties>
        <!-- Disable the second-level cache  -->
        <property name="hibernate.cache.use_second_level_cache" value="false"/>

        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <!--<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>-->
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TestService"/>
        <property name="hibernate.connection.username" value="xxxxx"/>
        <property name="hibernate.connection.password" value="xxxxx"/>
        <property name="hibernate.connection.pool_size" value="1"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <!-- Default is false for backwards compatibility.  Should be used on all new projects -->
        <property name="hibernate.id.new_generator_mappings" value="true"/>
    </properties>
</persistence-unit>

我使用MySQL和Derby数据库复制了这个问题。

以下是失败的更新尝试示例:

public boolean testUpdate(DurableUser user) {
    entityManager.get().getTransaction().begin();

    String testUpdateString = "askdjfaskdjfalsdkf";
    user.setField(testUpdateString);

    entityManager.get().persist(user);
    log.info("user field persisted");
    entityManager.get().flush();
    entityManager.get().getTransaction().commit();

    entityManager.get().clear();
    return true;
}

它需要DurableUser(在之前的http会话中创建w / o问题)并更新字段。即使使用明确的flush()commit(),hibernate也不会发布更新语句。

我在日志中注意到org.hibernate.internal.util.EntityPrinter会记录用户的toString(),其中显示了更新的字段。这是否意味着hibernate确实认识到实体已被弄脏并且仍然没有持续改变?

有人可以回答为什么我可以成功创建新实体但不更新现有实体吗?到目前为止,我完全被难过了。

编辑: 以下是会话中的日志:

2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7    spi.AbstractTransactionImpl - begin
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7           jdbc.JdbcTransaction - initial autocommit status: true
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7           jdbc.JdbcTransaction - disabling autocommit
2013-12-04 20:56:10,881  INFO http-apr-8080-exec-7                    dao.UserDao -  user field persisted
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Processing flush-time cascades
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Dirty checking collections
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.cards#7], was: [com.xxx.server.database.DurableUser.cards#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.donations#7], was: [com.xxx.server.database.DurableUser.donations#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.installments#7], was: [com.xxx.server.database.DurableUser.installments#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 3 collections
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7             util.EntityPrinter - Listing entities:
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7             util.EntityPrinter - com.xxx.server.database.DurableUser{donations=[], installments=[], id=7, username=test, name=test testerson, passwordChangeKey=askdjfaskdjfalsdkf, cards=[]}
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7    spi.AbstractTransactionImpl - committing
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Processing flush-time cascades
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Dirty checking collections
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.cards#7], was: [com.xxx.server.database.DurableUser.cards#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.donations#7], was: [com.xxx.server.database.DurableUser.donations#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7           internal.Collections - Collection found: [com.xxx.server.database.DurableUser.installments#7], was: [com.xxx.server.database.DurableUser.installments#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2013-12-04 20:56:10,884 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 3 collections
2013-12-04 20:56:10,884 DEBUG http-apr-8080-exec-7             util.EntityPrinter - Listing entities:
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7             util.EntityPrinter - com.xxx.server.database.DurableUser{donations=[], installments=[], id=7, username=test, name=test testerson, passwordChangeKey=askdjfaskdjfalsdkf, cards=[]}
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7           jdbc.JdbcTransaction - committed JDBC Connection
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7           jdbc.JdbcTransaction - re-enabling autocommit
2013-12-04 20:56:11,613 DEBUG http-apr-8080-exec-7 internal.LogicalConnectionImpl - Releasing JDBC connection
2013-12-04 20:56:11,614 DEBUG http-apr-8080-exec-7 internal.LogicalConnectionImpl - Released JDBC connection

0 个答案:

没有答案