users@glassfish.java.net

Re: How to shut up GlassFish being too smart (JPA + Spring)?

From: Ryan de Laplante <ryan_at_ijws.com>
Date: Sun, 25 Jan 2009 12:23:10 -0500

I've been through this recently. There are two approaches to this problem:

1) Make Spring manage everything. Spring will be the entity manager
factory. Spring will manage transactions with Jpa's local transaction
manager. Spring will manage the DataSource, etc. You will need to use
spring-agent.jar in JVM options when starting GlassFish, use load time
weaving, and have a ton of Spring XML configuration. This is suitable
for bare bones servlet containers like Tomcat.


2) Integrate Spring with the existing infrastructure. Let GlassFish be
the entity manager factory, let GlassFish manage transactions, let
GlassFish use JTA transaction manager, let GlassFish be in charge of the
DataSource, etc. This requires minimal configuration in Spring, and no
headaches.


in persistence.xml :

<persistence-unit name="MyPU" transaction-type="JTA">
    <jta-data-source>jdbc/myDataSource</jta-data-source>
    <class>com.company.project.model.Model1</class>
    <class>com.company.project.model.Model2</class>
    <class>com.company.project.model.Model3</class>
    <class>com.company.project.model.Model4</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="toplink.logging.level.sql" value="INFO"/>
        <property name="toplink.cache.shared.default" value="false"/>
        <property name="toplink.cache.shared.Model1" value="true"/>
        <property name="toplink.cache.shared.Model3" value="true"/>
    </properties>
</persistence-unit>


Note that I specified a JNDI datasource which must exist in GlassFish.
Next, you only need the following configuration in Spring's
applicationContext.xml to make it work:

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

<tx:jta-transaction-manager/>
<tx:annotation-driven/>

GlassFish (and maybe all Java EE 5 app servers?) exports JPA persistence
units in JNDI as "persistence/{PUname}", so we tell Spring that any time
it sees a @PersistenceContext or @PersistenceUnit annotation, get the
EntityManager or EntityManagerFactory from GlassFish. Spring's
@Transactional annotation will work with this setup.

I have multiple persistent units, and I simply added additional map
entry lines in Spring config to be able to use them.

You can also add a <property name="defaultPersistenceUnitName"
value="MyPU"/> line to the configs.


Ryan


glassfish_at_javadesktop.org wrote:
> Okey...
>
> I want Glassfish to manage stuff, but NOT my persistence unit stuff, which I want to do Spring way. Now I have my class annotated...
>
> @PersistenceContext
> public void setEntityManager(EntityManager entityManager) {
> this.entityManager = entityManager;
> }
>
> PU is
>
> <persistence-unit name="WebStorePU">
> <provider>org.hibernate.ejb.HibernatePersistence</provider>
> ...
>
> Sprig ApplicationContxt
>
> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
> <property name="dataSource" ref="webStoreDataSource"/>
> <property name="loadTimeWeaver">
> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
> </property>
> </bean>
>
> <bean id="webStoreDao" class="org.objectmaster.webstore.dao.WebStoreDao"/>
>
> GlassFish is too smart and finds persistence unit and registers it to itself?
> WHERE? It does not appear under JNDI tree, so I can't access it in spring through JNDI
>
> Better choice would be for me to have this PU file named somehow different so GlassFish does not tries to be too smart, but it IS too smart, as it notcies that my classes are annotated with PersistenceContext and tries to find PU and fails... How to disable this stuff, how to make GlassFish not to do what I don't want to do... or if it does, then how to make it do as normal ppl do...
> [Message sent by forum member 'janis_olekss' (janis_olekss)]
>
> http://forums.java.net/jive/thread.jspa?messageID=328061
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>
>