ここに書いたようにQuerydsl(4.1.4)でJPAを問い合わせようとしています http://www.querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration。私は、JPAバックエンドとしてHibernate(5.2.12.Final)を使用します。 com.querydsl.apt.jpa.JPAAnnotationProcessor
プロセッサを搭載したapt-maven-plugin
を使用して、JPAアノテーション付きクラスからQuerydslクエリタイプを生成します。Querydslを使用してJPAエンティティを更新するにはどうすればよいですか?
エンティティの更新中に問題が発生しました。更新された値はJavaコードには表示されません。
final EntityManagerFactory entityManagerFactory = PersistenceTestUtils.buildEntityManagerFactory();
final EntityManager entityManager = entityManagerFactory.createEntityManager();
final EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
final QJpaUpdateRound qJpaUpdateRound = QJpaUpdateRound.jpaUpdateRound;
final JPQLQueryFactory queryFactory = new JPAQueryFactory(entityManager);
final QJpaUpdateRound qJpaUpdateRound = QJpaUpdateRound.jpaUpdateRound;
final JPQLQueryFactory queryFactory = new JPAQueryFactory(entityManager);
final long roundId = 3L;
System.out.println("Original " +originalRound);
queryFactory //
.update(qJpaUpdateRound) //
.set(qJpaUpdateRound._success, true) //
.where(qJpaUpdateRound._id.eq(roundId)) //
.execute();
// entityManager.clear(); // Workaround
// Fetch the updated value
final UpdateRound updatedRound = queryFactory //
.selectFrom(qJpaUpdateRound) //
.where(qJpaUpdateRound._id.eq(roundId)) //
.fetchOne();
transaction.commit();
System.out.println("Updated "+ updatedRound);
このプリント:ここでエンティティのbooleanプロパティ(success
と命名)を更新する関連するコードスニペットである
オリジナルJpaUpdateRound {ID = 3。インスタント= 2017-11-06T19:27:01.141Z; 成功= false}
更新JpaUpdateRound {id = 3; インスタント= 2017-11-06T19:27:01.141Z;成功= FALSE}
また、originalRound
とupdatedRound
に同一の試験は、両方の変数が同じインスタンスであることを示しています。
データベースをチェックインしました。値は実際に更新されています。そして、私は
entityManager.clear(); // Workaround
を次の行のコメントを解除した場合、プログラムが印刷
オリジナルJpaUpdateRound {ID = 3。インスタント= 2017-11-06T19:39:01.038Z; 成功= false}
更新JpaUpdateRound {id = 3; インスタント= 2017-11-06T19:39:01.038Z;成功=真}
これは私が期待している結果です。
Querydslの更新が実行されると、エンティティキャッシュが更新されないようです。したがって、元のJavaオブジェクトのみを返します。
なぜですか? JPAバックエンドとQuerydslをどのように同期させることができますか?