我正在尝试为我的持久性代码创建一个单元测试。我正在使用带有 JPA 注释的 Hibernate。我没有persistence.xml(在所有关于JPA 单元测试的文章中都使用了它)。我不想使用spring或创建persistence.xml,因为我有很多持久性类,并且所有这些类的hibernate初始化都需要很多时间,所以我想显式地将类添加到hibernate。
我也不能configuration.createSessionFactory()
像在 Hibernate 单元测试文章中推荐的那样使用它,因为我的 DAO 有 Spring 注入的 JPA EntityManager。
所以我正在使用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 为真)。看起来 AUTOCOMMIT 是错误的(在生产环境中,Spring 管理事务)。如您所见,我将配置设置为 AUTOCOMMIT true,我什至从 EntityManager 检索 JDBC 连接,并且在调试器中看到,自动提交为 true,但只有在我的单元测试中我明确开始并提交事务时才会触发插入。
我做错了什么?如何让我的测试在自动提交中运行?
谢谢!