Chapter 7. Migrating Spring Applications
7.1. Migrating Spring Archives to Red Hat JBoss Enterprise Application Platform 6 from a Previous Version
Overview
This section covers the main points you must considered when you plan to migrate existing Spring applications from JBoss Enterprise Application Platform 5 to JBoss Enterprise Application Platform 6. The changes required depend on the strategy and features used by the application.
Important
This example is intended to help you get your application running on JBoss Enterprise Application Platform 6 as a first step. Be aware that this configuration is not supported, so upgrade your application to use Hibernate 4 as your next migration step.
Update Spring applications that use Hibernate
Applications that use the Hibernate API directly with Spring, through either LocalSessionFactoryBean or AnnotationSessionFactoryBean, may use a version of Hibernate 3 packaged inside the application. Hibernate 4, which is provided through the org.hibernate module of JBoss Enterprise Application Platform 6, is not supported by Spring 3.0, but it is supported by Spring 3.1 or higher.
Update Spring applications that use JPA
The changes required in the application depend on whether the Spring JPA based application uses a server-deployed persistence unit or a Spring-managed persistence unit.
7.1.1. Server-deployed Persistence Unit
Applications that use a server-deployed persistence unit must observe the typical Java EE rules with respect to dependency management. The
javax.persistence classes and the persistence provider, in this case Hibernate, are contained in modules which are added automatically by the application when the persistence unit is deployed. To use the server-deployed persistence units from within Spring, do one of the following:
- Register the JNDI persistence context in the
web.xmlfile: <persistence-context-ref> <persistence-context-ref-name>persistence/petclinic-em</persistence-unit-ref-name> <persistence-unit-name>petclinic</persistence-unit-name> </persistence-context-ref>
The persistence context is then available for JNDI lookup as follows: <jee:jndi-lookup id="entityManager" jndi-name="java:comp/env/persistence/petclinic-em" expected-type="javax.persistence.EntityManager"/>
- Register the JNDI persistence unit in the
web.xmlfile: <persistence-unit-ref> <persistence-unit-ref-name>persistence/petclinic-emf</persistence-unit-ref-name> <persistence-unit-name>petclinic</persistence-unit-name> </persistence-unit-ref> <persistence-unit-ref> <persistence-unit-ref-name>persistence/petclinic-emf</persistence-unit-ref-name> <persistence-unit-name>petclinic</persistence-unit-name> </persistence-unit-ref>
The persistence unit is then available for JNDI lookup as follows: <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/petclinic-emf" expected-type="javax.persistence.EntityManagerFactory"/>
Note
JNDI binding using the
persistence.xml file properties is not supported in JBoss Enterprise Application Platform 6.
7.1.2. Spring-managed Persistence Unit
Spring applications running on JBoss Enterprise Application Platform 6 may also create persistence units using the
LocalContainerEntityManagerFactoryBean class. For such applications consider the following:
- Change the location of persistence unit definitionsWhen a deployment contains a
META-INF/persistence.xmlfile or aWEB-INF/classes/META-INF/persistence.xmlfile, the application server attempts to create a persistence unit based on the data in that file. In most cases, these definition files are not compliant with the Java EE requirements because the required elements (for example the datasource of the persistence unit) are expected to be provided by the Spring context definitions. This results in failure of deployment of the persistence unit and consequently of the entire deployment.You can avoid this problem by using a feature of theLocalContainerEntityManagerFactoryBeanthat is designed for this purpose. Persistence unit definition files can exist in other locations than theMETA-INF/persistence.xmlfile and the location can be indicated through thepersistenceXmlLocationproperty of the factory bean class.The following example assumes the persistence unit is in theMETA-INF/jpa-persistence.xmlfile: <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/> <!-- other definitions --> </bean>
- Manage dependenciesIf using Hibernate with the application, the Hibernate 3 JARs must be included in the deployment. However, due to the presence of @PersistenceUnit or @PersistenceContext annotations on the application classes, the application server automatically adds the Hibernate 4
org.hibernatemodule as a dependency. You can instruct the server to exclude this module by creating aMETA-INF/jboss-deployment-structure.xmlfile (or aWEB-INF/jboss-deployment-structure.xmlfile for web applications) with the following content:<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2"> <deployment> <exclusions> <module name="org.hibernate"/> </exclusions> </deployment> </jboss-deployment-structure>