0

假设我想对现有的 JPA 方案执行单个数据操作,例如为每个实体创建 n 个实体Student。我添加了处理学生的功能,我想从 CSV 文件中加载它们(只是一个虚构的例子)。当然,我已经准备好了实体和表格。

现在如何在 EJB 范围内做到这一点?

通常使用 Spring 我会简单地编写一个单元测试,它使用 @RunWith(SpringJUnit4ClassRunner.class)注释来加载上下文,其中初始化持久性。我无法在 EJB 中执行此操作,因为我依赖 Java EE 容器为我提供数据源和PersistenceContext.

4

1 回答 1

0

You can use JPA outside of application containers. When running within an application container, you're using container-managed entity managers. You can use JPA in plain JavaSE applications. In those cases, you want an application-managed entity manager. Which is using the javax.persistence.Persistence class to get the EntityManagerFactory (from which you get the EntityManager).

Once you have the entityManager, you can perform the same code. All you need to do in your tests is ensure you can get at the Persistence class, and you should be all good.

于 2013-03-24T09:54:51.760 回答