永続化コードの単体テストを作成しようとしています。JPA アノテーション付きの Hibernate を使用しています。persistence.xml はありません (JPA 単体テストに関するすべての記事で使用されています)。多くの永続化クラスがあり、それらすべての休止状態の初期化に時間がかかるため、Spring を使用したり、persistence.xml を作成したりしたくないため、明示的に休止状態にするクラスを追加したいと考えています。
またconfiguration.createSessionFactory()
、私のDAOにはSpringによって注入されたJPA EntityManagerがあるため、Hibernate単体テスト記事で推奨されているように使用できません。
だから私は使用していEntityManagerFactoryImpl
ます:
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.setProperty(Environment.DRIVER,"org.apache.derby.jdbc.EmbeddedDriver");
configuration.setProperty(Environment.URL,"jdbc:derby:memory:srf.derby;create=true");
configuration.setProperty(Environment.USER, "");
configuration.setProperty(Environment.DIALECT, DerbyDialect.class.getName());
configuration.setProperty(Environment.SHOW_SQL, "true");
configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperty( Environment.AUTOCOMMIT, "true");
configuration.addAnnotatedClass(MyPersistentClass.class);
MyHibernateDAO dao = new MyHibernateDAO();
EntityManagerFactoryImpl entityManagerFactory = new EntityManagerFactoryImpl(
configuration.buildSessionFactory(),
PersistenceUnitTransactionType.RESOURCE_LOCAL,
true,
null,
configuration);
dao.setEntityManager(entityManagerFactory.createEntityManager());
これは問題ないように見えますが、何らかの理由で db への挿入は発生しませんが、すべての選択は問題ありません (SQL を true に表示しました)。AUTOCOMMIT が false のようです (本番環境では、Spring がトランザクションを管理します)。ご覧のとおり、構成を AUTOCOMMIT true に設定し、EntityManager から JDBC 接続を取得し、デバッガーで自動コミットが true であることを確認しましたが、単体テストでトランザクションを明示的に開始してコミットした場合にのみ挿入が起動されます。
私が間違っていることは何ですか?自動コミットでテストを実行するにはどうすればよいですか?
ありがとうございました!