0 Replies Latest reply on Mar 25, 2010 2:40 PM by Barry Sheward

    How can I share Entities between JBoss AS and J2SE?

    Barry Sheward Newbie

      Hi,

       

      I have a package full of Entity Beans (annotated with @Entity and @Table). This package exists within a JAR which until now has been deployed as part of a EAR into JBoss AS. The persistence.xml in the JAR is:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                   version="1.0">
                  
         <persistence-unit name="abc">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:abc/datastore/ABCDS</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="validate"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
            </properties>
         </persistence-unit>
         
      </persistence>

       

      I want to use the same set of Entity Beans in a J2SE application, but I've run into some problems. It seems that I cannot define 2 persistence units in the same JAR, so under time contraints, I copied my Entity Bean classes, used the following persistence.xml in a different JAR file:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                   version="1.0">
                  
         <persistence-unit name="abc-se">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
         </persistence-unit>
         
      </persistence>

       

      With the following code:

       

                  Map configOverrides = new HashMap();
                  configOverrides.put( "hibernate.dialect", "org.hibernate.dialect.SQLServerDialect" );
                  configOverrides.put( "hibernate.connection.driver_class", "net.sourceforge.jtds.jdbc.Driver" );
                  configOverrides.put( "hibernate.connection.username", user );
                  configOverrides.put( "hibernate.connection.password", password );
                  configOverrides.put( "hibernate.connection.url", url );
                 
                  LOGGER.info(  "Create EntityManagerFactory" );
                  EntityManagerFactory emf = Persistence.createEntityManagerFactory( "abc-se", configOverrides );
                 
                  LOGGER.info(  "Create EntityManager" );
                  em = emf.createEntityManager(); // Retrieve an application managed entity manager

       

       

      So, my question is, how can I share a single JAR of Entities across a J2EE application? I can see a few options:

      1. Create a JNDI based datasource in my J2SE application, and use the "abc" persistence unit.
      2. Use the "abc" persistence unit in my J2SE application, but somehow remove the JNDI reference and use the configOverrides map before creating the EntityManager.
      3. Find some way to define 2 persistence units in the same JAR
      4. Something else?

       

      Can anybody offer any advice?

       

      Thanks and Regards,

       

      Barry