0

我在 jboss 5.1GA 中部署了我的 ear-project。

从 webapp 我没有问题,我的 ejb3 的查找工作正常!

埃斯:

ShoppingCart sc= (ShoppingCart) 
(new InitialContext()).lookup("idelivery-ear-1.0/ShoppingCartBean/remote");

我的 EntityManager 也可以正常工作!

@PersistenceContext
private EntityManager manager;

从测试环境(我使用 Eclipse)中查找相同的 ejb3 工作正常!但是 entitymanager 或 PersistenceContext 的查找不起作用!!!

我的好测试用例:

 public void testClient() {

  Properties properties = new Properties();
  properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
  properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
  properties.put("java.naming.provider.url","localhost");  

  Context context;
  try{
   context = new InitialContext(properties);
   ShoppingCart cart = (ShoppingCart) context.lookup("idelivery-ear-1.0/ShoppingCartBean/remote"); // WORK FINE
  } catch (Exception e)  {
   e.printStackTrace();
  }
 }

我的坏测试:

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("idelivery"); 
   EntityManager em = emf.createEntityManager(); //test1


   EntityManager em6 = (EntityManager) new InitialContext().lookup("java:comp/env/persistence/idelivery"); //test2


   PersistenceContext em3 = (PersistenceContext)(new InitialContext()).lookup("idelivery/remote"); //test3

我的persistence.xml

<persistence-unit name="idelivery" transaction-type="JTA">
    <jta-data-source>java:ideliveryDS</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create-drop" /><!--validate | update | create | create-drop-->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>

我的数据源:

    <datasources>
    <local-tx-datasource>
        <jndi-name>ideliveryDS</jndi-name>
                    ...
    </local-tx-datasource>
    </datasources>

在构建 ejb 之前,我需要 EntityManager 和 PersistenceContext 来测试我的查询...

我的错误在哪里?

4

2 回答 2

0

服务器端 EntityManager 无法序列化,因此您可以将其用作客户端 EntityManager。这意味着在客户端引用的 EntityManager 仍然可以与数据库通信、使用连接池等。这是不可能的(例如保护数据库服务器的防火墙)。

如果您需要测试 JPA,请使用没有 JTA 事务的本地 EntityManager。如果要测试 EJB,则需要模拟整个 EJB 容器。您可以使用 Spring Pitchfork 或 Glassfish 3 嵌入式容器(后一种选择更容易)。

于 2010-03-05T20:23:35.970 回答
0

我需要测试 JPA,使用没有 JTA 事务的本地 EntityManager!

我听从了你的建议:我用一个新的持久性单元创建了新的 persistence.xml

<persistence-unit name="ideliveryTest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>it.idelivery.model.Category</class>
    <class>it.idelivery.model.User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/application"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
    </properties>
</persistence-unit>

在我的测试用例中:

    try {
        logger.info("Building JPA EntityManager for unit tests");
        emFactory = Persistence.createEntityManagerFactory("ideliveryTest");
        em = emFactory.createEntityManager();
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Exception during JPA EntityManager instanciation.");
    }

工作正常!

在我的 maven 项目中,我将 persistence.xml 与 persistence-unit type="RESOURCE_LOCAL" 放在 src/test/resources

我将 persistence.xml 与 persistence-unit type="JTA" 放在 src/main/resources

通过这种方式,我有两个独立的环境。一种用于测试,一种用于生产。

这是最佳实践吗?

于 2010-03-08T09:38:32.043 回答