0

私は多くの人と同じ問題を抱えていると思いますが、ほとんどの場合未解決です。とにかく試してみます。皆さんが私を助けてくれることを願っています。

@persistenceContext アノテーションを使用して que Entity Manager を注入しようとすると、リポジトリに問題があり、常に null になります。

スタック: Spring 4.2.5 Spring Data 1.10.1 Hibernate 5

これは、スプリント データの私の 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>
4

1 に答える 1