2010-03-05 36 views
0

我有我的ear-project部署在jboss 5.1GA。關於jboss查找entitymanager的問題

從webapp我沒有問題,查找我的ejb3工作正常!

ES:

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

也是我的EntityManager做工精細iniection!

@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> 

我需要EntityManager和PersistenceContext在構建ejb之前測試我的查詢...

我的錯誤在哪裏?

回答

0

服務器端EntityManager不能被序列化,以便您可以將其用作客戶端EntityManager。這意味着在客戶端引用的EntityManager仍然可以與數據庫交談,使用連接池等。這是不可能的(例如,考慮防火牆,它可以保護數據庫服務器)。

如果您需要測試JPA,請使用沒有JTA事務的本地EntityManager。如果你想測試EJB,你需要模擬整個EJB容器。您可以使用Spring Pitchfork或Glassfish 3嵌入式容器(後者更容易)。

+0

謝謝!我現在會嘗試。 – Stefano

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中與SRC /測試/資源持久單元類型=「RESOURCE_LOCAL」

和我把具有持久性單元類型=「JTA」的persistence.xml中的src /主要/資源

通過這種方式我有兩個分隔環境。一個用於測試,一個用於生產。

這是最佳做法?