users@glassfish.java.net

Re: glassfish-embedded-shell.jar 3.0.1 deployment issue

From: <glassfish_at_javadesktop.org>
Date: Tue, 22 Jun 2010 15:25:09 PDT

According to the schema for web.xml I can define a persistence-contect-ref that would allow me access to the EntityManager that would allow me to inject it into a Servlet.

<persistence-context-ref>
 <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
  <persistence-unit-name>my-pu</persistence-unit-name>
</persistence-context-ref>

And according to ejb-jar.xml schema, I could define a persistence-context-ref that would allow me access to the EntityManager in an EJB that I could define in my test classes of my project.

    <enterprise-beans>
        <session>
            <ejb-name>AppTest</ejb-name>
            <ejb-class>org.acme.AppTestBean</ejb-class>
            <session-type>Stateless</session-type>
            <persistence-context-ref>
                <persistence-context-ref-name>persistence/my-pu/em</persistence-context-ref-name>
                <persistence-unit-name>my-pu</persistence-unit-name>
            </persistence-context-ref>
            <persistence-unit-ref>
                <persistence-unit-ref-name>persistence/my-pu/factory</persistence-unit-ref-name>
                <persistence-unit-name>my-pu</persistence-unit-name>
            </persistence-unit-ref>
        </session>
    </enterprise-beans>

And if I were to employ an interceptor on my Stateless EJB, I could allow Spring to Autowire any addition dependencies of my EJB that might be contained within the Spring context.

@Interceptors(SpringBeanAutowiringInterceptor.class)

So it would seem that at that point, I could perform a JNDI lookup based upon the scoping rules associated to the JEE component which is the EJB.

And incase anyone is still interested in how to get all of this working with the embedded-glassfish-all plugin and Spring, here are the steps.

[u]pom.xml dependencies:[/u]

        <dependencies>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-tx</artifactId>
                        <version>${org.springframework.version}</version>
                </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                        <version>${org.springframework.version}</version>
                </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-test</artifactId>
                        <version>${org.springframework.version}</version>
                </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-orm</artifactId>
                        <version>${org.springframework.version}</version>
                </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.0.0</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.8.1</version>
                        <scope>test</scope>
                </dependency>

        <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

                <dependency>
                        <groupId>javax</groupId>
                        <artifactId>javaee-api</artifactId>
                        <version>6.0</version>
                        <scope>provided</scope>
                </dependency>
        </dependencies>


[u]AppTest class:[/u]

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/test-applicationContext.xml" ,"applicationContext.xml"})
@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class AppTest extends TestCase {

    private static EJBContainer ejbContainer;

    /**
     * @throws Exception
     */
    @BeforeClass
    public static void setUpClass() throws Exception {
            System.getProperties().put("glassfish.embedded.tmpdir", System.getProperty("java.io.tmpdir"));
            java.util.Map<String,String> map = new java.util.HashMap<String,String>();
            map.put( EJBContainer.APP_NAME, "MyAppTest");
            ejbContainer = EJBContainer.createEJBContainer(map);
    }

    /**
     * @throws Exception
     */
    @AfterClass
    public static void tearDownClass() throws Exception {
            if(ejbContainer != null) {
                    ejbContainer.close();
            }
    }


[u]ejb-jar.xml located in src/test/resources/META-INF[/u]
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
    <module-name>myPersistenceEjbModule</module-name>

    <enterprise-beans>
        <session>
            <ejb-name>AppTest</ejb-name>
            <local-bean />
            <ejb-class>com.youcompany.test.AppTest</ejb-class>
            <session-type>Stateless</session-type>
        </session>
    </enterprise-beans>
</ejb-jar>


[u]persistence.xml located in src/main/resources/META-INF[/u]

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="my-pu" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/myDataSource</jta-data-source>
    <class>com.mycompany.persistence.entity.SomeEntity</class>
    
    <shared-cache-mode>ALL</shared-cache-mode>

    <properties>
      <property name="eclipselink.weaving" value="static" />
      <property name="eclipselink.ddl-generation" value="create-tables"/>
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.logging.exceptions" value="true" />
      <property name="eclipselink.jdbc.native-sql" value="true"/>
      <property name="eclipselink.jdbc.cache-statements" value="true"/>
      <property name="eclipselink.target-database" value="Auto"/>
      <property name="eclipselink.jdbc.exclusive-connection.mode" value="Transactional"/>
      <property name="eclipselink.logging.logger" value="ServerLogger"/>
      <property name="eclipselink.weaving" value="static"/>
      <property name="eclipselink.target-server" value="SunAS9"/>

    </properties>
  </persistence-unit>
</persistence>


[u]applicationContext.xml located in src/main/resources[/u]

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <tx:jta-transaction-manager />

    <tx:annotation-driven/>
    
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaDialect">
            <bean
                class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
        </property>
        <property name="persistenceUnitName" value="my-pu"></property>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
        <property name="persistenceUnits">
            <map>
                <entry key="my-pu" value="persistence/my-pu"/>
            </map>
        </property>
        <property name="persistenceContexts">
            <map>
                <entry key="my-pu" value="persistence/my-pu"/>
            </map>
        </property>
    </bean>

    <context:component-scan base-package="com.mycompany.persistence" />
</beans>


[u]BaseDaoImpl located under src/main/java[/u]
@Transactional(propagation = Propagation.REQUIRED)
public abstract class BaseDaoImpl<T, PK extends java.io.Serializable>
    implements BaseDao<T, PK> {

    private JpaTemplate jpaTemplate;
    
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        jpaTemplate = new JpaTemplate(entityManager);
    }

    protected JpaTemplate getJpaTemplate() {
        return jpaTemplate;
    }


I hope this will save someone days worth of aggravation and frustration trying to figure out how to get all the pieces working together.
[Message sent by forum member 'fericit_bostan']

http://forums.java.net/jive/thread.jspa?messageID=475424