質問

私は、JBoss 5.1GAで展開私の耳のプロジェクトを持っています。

私は問題を持っていないWebアプリケーションから、私の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 ...

どこに間違いがある?

役に立ちましたか?

解決

あなたは、クライアント側のEntityManagerとしてそれを使用できるように、

サーバー側のEntityManagerをシリアライズすることはできません。それは、EntityManagerのは、(例えば、データベース・サーバを保護、ファイアウォール、と思う)まだデータベース、使用接続プールなどそれは不可能であると話をすることができ、クライアント側で参照することを意味します。

あなたはJPAをテストする必要がある場合は、

は、JTAトランザクションなしでローカルEntityManagerを使用します。あなたがテストEJBにしたい場合は、全体のEJBコンテナをシミュレートする必要があります。あなたは春のピッチフォークやGlassfishの3埋め込まれたコンテナを(後者のオプションが簡単です)を使用することができます。

他のヒント

私は、JTAトランザクションなしでローカルEntityManagerを使用し、JPAをテストする必要があります!

私はあなたの提案に従っ:私は新しい永続性ユニット

で新しいの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プロジェクトでは、私はSRC /テスト/リソースのpersistence-ユニットタイプ= "RESOURCE_LOCAL" とのpersistence.xmlを置く

と私は永続ユニットのタイプとのpersistence.xmlを置く= SRC /メイン/リソースの "JTA"

この方法で私は2つのセパレートの的環境を持っています。生産のためのテストと一対一ます。

これがベストプラクティスです?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top