如果没有像Spring这样的容器(或诸如基于Spring的Unitils之类的容器),则必须手动注入实体管理器。在这种情况下,您
可以 将以下内容用作基类:
public abstract class JpabaseRolledBackTestCase { protected static EntityManagerFactory emf; protected EntityManager em; @BeforeClass public static void createEntityManagerFactory() { emf = Persistence.createEntityManagerFactory("PetstorePu"); } @AfterClass public static void closeEntityManagerFactory() { emf.close(); } @Before public void beginTransaction() { em = emf.createEntityManager(); em.getTransaction().begin(); } @After public void rollbackTransaction() {if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } if (em.isOpen()) { em.close(); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)