4

I am using JPA and spring to connect my JBOss server to an oracle database.

Here are my configurations:

database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.example" />

    <!-- Add JPA support -->
    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="unitDS" />
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <!-- Add Transaction support -->
    <bean id="mtsTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
    </bean>

    <!-- Use @Transaction annotations for managing transactions -->
    <tx:annotation-driven transaction-manager="mtsTxManager"
        proxy-target-class="true" />

</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- @version $Date: 2010-06-04 15:50:29 +0200 (Fri, 04 Jun 2010) $ | $LastChangedBy: 
    ext_computaris_eprager $ | LastChangedRevision: $Rev: 2424 $ -->
<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="unitDS"
        transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:jboss/datasources/unitDS</non-jta-data-source>


        <class>com.example.foo</class>
        <class>com.example.bar</class>



        <!-- properties specific for the underlying implementation of the JPA EntityManager 
            (Hibernate) -->
        <properties>
            <!-- ////////////// HBM/DDL related properties ////////////// were removed 
                from here (set as system properties in tests) -->
            <!-- NCA -->
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="jboss.entity.manager.factory.jndi.name"
                value="java:/locationserverEntityManagerFactory" />

            <!-- ////////////// Enable JTA transaction ////////////// -->
            <property name="transaction.factory_class"
                value="org.hibernate.transaction.JTATransactionFactory" />
            <property name="jta.UserTransaction" value="java:comp/UserTransaction" />
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.JBossTransactionManagerLookup" />
            <!-- ////////////// debugging/testing related properties ////////////// -->
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />

            <!-- 2nd level cache -->
<!--            <property name="hibernate.cache.provider_class" -->
<!--                value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" /> -->
<!--            <property name="net.sf.ehcache.configurationResourceName" -->
<!--                value="/ehcache.xml" /> -->
<!--            <property name="hibernate.cache.use_query_cache" value="true" /> -->
<!--            <property name="hibernate.cache.use_second_level_cache" -->
<!--                value="true" /> -->
<!--            <property name="hibernate.generate_statistics" value="true" /> -->
        </properties>
    </persistence-unit>

</persistence>

The spring context is initialized normally. I got the message :

Starting persistence unit unitDS

When I start my server.

I also have an abstract dao JPA where I inject my context using annotation @persistenceContext:

public class AbstractDaoJpa<E, PK> implements DaoBase<E, PK> {

    /**
     * 
     */
    @PersistenceContext(unitName="unitDS")
    @Qualifier(value = "entityManagerFactory")
    private EntityManager em;

The problem is when I am trying to connect to my database my EntityManager is always null.

I don't understand where this problem comes from. Do I have bad configuration ?

I tried several solutions of similar post but couldn't resolve my problem.

razafinr
  • 932
  • 2
  • 10
  • 15
  • 1
    possible duplicate of [Entitymanager is null spring JPA configuration](http://stackoverflow.com/questions/22853168/entitymanager-is-null-spring-jpa-configuration) – ruhungry Apr 15 '14 at 08:51
  • What happens if you remove the qualifier and/or the unitName? – Martin Frey Apr 15 '14 at 10:14
  • Tried to remove it, didn't change. The context is set up when i keep the @persistenceContext, otherwise not (which is normal). – razafinr Apr 15 '14 at 11:14

4 Answers4

0

I'm not sure, if this is the problem, but in your Spring configuration you have emf as bean name, and in the AbstractDao you have entityManagerFactory as qualifier. So i guess Spring fails to inject the correct bean.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • Sorry didn't fix the problem, I referenced emf for transaction support from the LocalContainerEntityManagerFactoryBean created above it. the problem doesn't come from this – razafinr Apr 15 '14 at 09:02
0

You don't seem to have added <context:component-scan base-package="your.package" /> which enables component scanning. Why don't you try that with the appropriate package

geoand
  • 60,071
  • 24
  • 172
  • 190
0

I know it's late, But I will write for future reference. There is a catch when you are creating a instance of a implementation using new keyword.For more detail you can check this thread

Community
  • 1
  • 1
rock_lee
  • 11
  • 3
0

it may have certain factors * in persistence.xml persistence provider is missing

  • after setting your applicationContext.xml with any config you are getting entitymanagerfactory i.e from LocalEntity,LocalContainer or JNDI Lookup .

    use persistenceContext

      private EntityManager em;
    

with class anotated with @transactional , @Repository

but you are still getting em = null the reason behind this is you are not calling your class using Dependency Injection .

Mirza
  • 33
  • 5