2016-04-14 5 views
0

私は多くの人と同じ問題を抱えていると思いますが、ほとんどの場合は未解決です。私はとにかく試してみましょう、みんなあなたが私を助けることを願っています。Spring 4 Jpa Hibernate - EntityManagerを挿入できない

@persistenceContext注釈を使用してque Entity Managerを挿入しようとすると、問題が私のリポジトリにあり、常にnullになります。

スタック: 春4.2.5 春データ1.10.1 Hibernateは5

これはスプリントデータのための私のxmlです:これは私のアプリケーションのcontext.xml

ある

<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"/> 

<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"/> 

これは私のレポジトリ

@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 ... 
} 

この質問はかなり古いですが、これは、我々が、同様にこの問題に直面し、我々のアプリケーションコンテキストにこのBeanを追加することによってそれを解決し、

<?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> 

答えて

0

私のpersistence.xmlのです:

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

context:annotation-configのドキュメントでは、これは必要ではないはずですが、私たちの場合はEclipseのVirgo 3.7の中でSpring 4.2をGemini Blueprintで使用していますが、この設定はおそらく主流とはかけ離れています。

関連する問題