Announcement Announcement Module
No announcement yet.
Configuring entityManagerFactory for Spring + Hibernate + EJB Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring entityManagerFactory for Spring + Hibernate + EJB

    I am currently converting my EJB 2.0 project to JPA 2.0 which uses Hibernate 3.6 as the vendor. Other environments are Spring 3.0.5, JBoss 6.0.0 Final Application Server.

    I think the configuration of the applicatoinContext file needs to be changed because previously I only use EJB 2.0. I have added a persistence.xml file, which uses JNDI lookup to get a JTA datasource abcDS:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="" 
    xmlns:xsi="" version="2.0" 
        <persistence-unit name="persistenceUnit" transaction-type="JTA">
            <!-- <provider>In JBoss AS, the default and only supported/recommended
             JPA provider is Hibernate</provider>  -->
            	<!-- Omit the dialect and let Hibernate auto-detect it (works for
            	   Oracle 9i, 10g and SQL Server 2000, 2005, & 2008, but not Oracle
            	   11g; set the system property hibernate.dialect to
            	   "org.hibernate.dialect.Oracle10gDialect") -->
                <!--value='create' to build a new database on each run;
                    value='update' to modify an existing database;
                    value='create-drop' means the same as 'create', but also drops
                      tables when Hibernate closes;
                    value='validate' makes no changes to the database-->
                <property name="" value="validate"/>
                <!-- Use default naming strategy so all tables and columns use
                 camel case -->
                <property name="hibernate.ejb.naming_strategy"
    Then in applicationContext.xml I added this:

    		<!-- This JNDI name must match that in the icms-ds.xml file -->
    		<jee:jndi-lookup id="dataSource" jndi-name="java:abcDS"/>
        <!-- The Spring transaction manager (this implementation of Spring's
        PlatformTransactionManager interface autodetects the J2EE container's
        existing JTA transaction manager). The definition below creates a bean with
        the default name of "transactionManager". 
        <tx:jta-transaction-manager  />
    But when I tried to access an JPA entity I will get this error:

    Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
    Then I thought I might forget to set the entityManagerFactory, so I added this:

        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
            <property name="dataSource" ref="dataSource"/>
            <property name="persistenceUnitName" value="persistenceUnit"/>
            <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml"/>
    and tried to make the application server aware that I am using the persistenceUnit. But I get an error in deployment saying
    Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
            at org.hibernate.impl.SessionFactoryImpl.<init>(
            at org.hibernate.cfg.Configuration.buildSessionFactory(
            at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
    But I am using JTA transactionmanager. Why did Hibernate complain about that?

    Is it possible to use one single entitymanagerfactory to handle the transactions in both EJB 2.0 and JPA 2 entities? I am in the middle of converting the EJBs so I would prefer to see both of them are working correctly.