簡體   English   中英

Spring 4 Jpa Hibernate-無法注入EntityManager

[英]Spring 4 Jpa Hibernate - Can't Inject EntityManager

我想我和許多人有同樣的問題,但是在大多數情況下並沒有解決。 無論如何我都會嘗試,希望你們能幫助我。

當我嘗試使用@persistenceContext注釋注入que Entity Manager時,問題出在我的存儲庫中,並且始終為null。

堆棧:Spring 4.2.5 Spring Data 1.10.1 Hibernate 5

這是我的Sprint數據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.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"/>

這是我的資料庫

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

這是我的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>

盡管這個問題已經很老了,但我們也面臨這個問題,並通過將這個bean添加到我們的應用程序上下文中來解決了這個問題:

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

context:annotation-config的文檔明確指出這不是必須的,但是在我們的情況下是這樣(盡管我們在帶有Gemini Blueprint的Eclipse Virgo 3.7中使用了Spring 4.2,所以該設置可能與主流無關)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM