春季 4 Jpa 休眠 - 无法注入实体管理器



我想我和很多人都有同样的问题,但大多数情况下都没有解决。我无论如何都会努力,希望你们能帮助我。

当我尝试使用@persistenceContext注释注入que Entity Manager时,问题就出现在我的存储库中,并且总是为null。

堆栈:弹簧4.2.5弹簧数据1.10.1休眠5

这是我的xml for Sprint数据:

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

这是我的应用程序上下文.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的文档明确指出,这不应该是必要的,但在我们的案例中是必要的(尽管我们在Eclipse Virgo 3.7中使用了Spring 4.2和Gemini Blueprint,所以这种设置可能远不是主流)。

相关内容

  • 没有找到相关文章

最新更新