Announcement Announcement Module
Collapse
No announcement yet.
Configuring entityManagerFactory for Spring + Hibernate + EJB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    
        <persistence-unit name="persistenceUnit" transaction-type="JTA">
            <!-- <provider>In JBoss AS, the default and only supported/recommended
             JPA provider is Hibernate</provider>  -->
             <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/abcDS</jta-data-source>
            <properties>
            	<!-- 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="hibernate.hbm2ddl.auto" value="validate"/>
                <!-- Use default naming strategy so all tables and columns use
                 camel case -->
                <property name="hibernate.ejb.naming_strategy"
                   value="org.hibernate.cfg.DefaultNamingStrategy"/>
            </properties>
        </persistence-unit>
    </persistence>
    Then in applicationContext.xml I added this:

    Code:
    		
    		<!-- 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:

    Code:
    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:

    Code:
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                id="entityManagerFactory">
            <property name="dataSource" ref="dataSource"/>
            <property name="persistenceUnitName" value="persistenceUnit"/>
            <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml"/>
        </bean>
    and tried to make the application server aware that I am using the persistenceUnit. But I get an error in deployment saying
    Code:
    Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
            at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
            at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
    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.
Working...
X