0

I guess I have the same problem as many people, but unsolved on most of cases. I will try anyway, hope you guys can help me.

The problem is in my repository when I try to inject que Entity Manager using @persistenceContext annotation and always comes null.

The stack: Spring 4.2.5 Spring Data 1.10.1 Hibernate 5

This is my xml for Sprint data:

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="conquerPU"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="packagesToScan" value="com.conquer.module" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/conquer" />
        <property name="username" value="app" />
        <property name="password" value="10203040" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <jpa:repositories base-package="com.conquer.module" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>

This is my application context.xml

<context:annotation-config/>
    <context:component-scan base-package="com">
        <context:include-filter type="aspectj" expression="com.*" />
    </context:component-scan>

    <!-- a HTTP Session-scoped bean exposed as a proxy -->
    <bean id="sessionData" class="com.conquer.common.SessionData" scope="session">
        <aop:scoped-proxy/>
    </bean>

    <!--Hibernate persistence interceptor - Used for audit data-->
    <bean id="hibernateInterceptor" class="com.conquer.module.security.interceptor.HibernateInterceptor"></bean>

    <!--Application Context to be used anywhere-->
    <bean id="applicationContextProvder" class="com.conquer.common.ApplicationContextProvider"/>


    <!-- SpringMVC -->
    <import resource="spring-mvc.xml"/>

    <!-- SpringData -->
    <import resource="spring-jpa.xml"/>

    <!-- SpringSecurity -->
    <import resource="spring-security.xml"/>

This is my repository

@Repository
@Transactional
public class BaseRepositoryImpl<T, ID extends Serializable> implements BaseRepository<T, ID> {

    @PersistenceContext
    public EntityManager em;

    public RepositoryFactorySupport baseFactory;
    public BaseRepository<T, ID> baseRepository;

    public BaseRepositoryImpl() {
        System.out.println("BASE REPOSITORY RUNNING...");
        this.baseFactory = new JpaRepositoryFactory(em);
        this.baseRepository = this.baseFactory.getRepository(BaseRepository.class);
    }

//    Implementations here ...
}

This is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="conquerPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
      <property name = "hibernate.show_sql" value = "true" />
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.ejb.interceptor" value="com.conquer.module.security.interceptor.HibernateInterceptor"/>
    </properties>
  </persistence-unit>
</persistence>
Glauter Lemos
  • 45
  • 1
  • 6

1 Answers1

1

Although this question is quite old, we faced with this problem as well and solved it by adding this bean to our application context:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

The documentation for context:annotation-config clearly states that this should not be necessary, but in our case it was (albeit we use Spring 4.2 inside Eclipse Virgo 3.7 with Gemini Blueprint, so this setup is probably far from mainstream).

svenpanko
  • 180
  • 8