views:

458

answers:

2

I have my ear-project deployed in jboss 5.1GA.

From webapp i don't have problem, the lookup of my ejb3 work fine!

es:

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

also the iniection of my EntityManager work fine!

@PersistenceContext
private EntityManager manager;

From test enviroment (I use Eclipse) the lookup of the same ejb3 work fine! but the lookup of entitymanager or PersistenceContext don't work!!!

my good test case:

 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();
  }
 }

my bad test :

   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

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

my datasource:

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

I need EntityManager and PersistenceContext to test my query before build ejb...

Where is my mistake?

A: 

Server-side EntityManager cannot be serialized so that you can use it as a client side EntityManager. That would mean that EntityManager referenced on the client-side still can talk to the database, use connection pool, etc. It is impossible (think of firewall, which protects database server, for instance).

If you need to test JPA, use local EntityManager without JTA transactions. If you want to test EJBs you need to simulate whole EJB container. You can use Spring Pitchfork or Glassfish 3 embedded container (the latter option is easier).

Piotr Kochański
thanks! I will try now.
Stefano
A: 

I need to test JPA, use local EntityManager without JTA transactions!

I followed your suggestion:I created new persistence.xml with a new persistence-unit

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

and in my test case:

    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.");
    }

work fine!

In my maven project i put persistence.xml with persistence-unit type="RESOURCE_LOCAL" in src/test/resources

and i put persistence.xml with persistence-unit type="JTA" in src/main/resources

by this way I have two separates enviroment. One for test and one for production.

it's a best practice?

Stefano