1

エンティティの値を更新してその更新を永続化しようとする guice-persist および guice-servlet (http-request スコープの jpa セッション) に問題がありますが、更新はデータベースに永続化されません。entityManager.flush()andを使用して書き込みを強制しようとしましたentityManager.getTransaction().commit()が、ログを見ると、http セッションが終了して jdbc 接続が解放されても、何も起こらないようです。
通常、休止状態が sql update ステートメントを発行することを期待しますが、更新は登録されないようです。私が奇妙に感じるのは、新しいエンティティの作成に問題がないことです。これは更新に影響しているように見えるだけです。

注入された UserDao を使用する Singleton スコープのサーブレットがあります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 セッションで問題なしで作成された) を取得し、フィールドを更新します。明示的なflush()andがあってもcommit()、休止状態は update ステートメントを発行しません。

更新されたフィールドを示すorg.hibernate.internal.util.EntityPrinterユーザーの をログに記録するログに気付きました。toString()それは、休止状態がエンティティが汚れていることを認識し、まだ変更を保持していないことを意味しますか?

新しいエンティティを正常に作成できるのに、既存のエンティティを更新できない理由を誰でも答えることができますか? これまでのところ、私は完全に困惑しています。

編集:セッションからのログは次のとおりです。

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
4

0 に答える 0