在CrudRepository方法中配置多个JPA存储库

时间:2014-09-29 09:50:52

标签: spring jpa crud

我正在尝试在我的Web应用程序中配置多个JPA存储库。目前我有一个存储库,如下所述。我正在使用Crud Repository

<jpa:repositories base-package="com.mypackage1" factory-class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory">
    <repository:include-filter type="assignable" expression="com.MyFirstRepository"/>
</jpa:repositories>

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.mypackage1.Model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
</bean> 

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

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:database/~test" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>     

如果我需要配置多个存储库,

             

<bean id="entityManagerFactory2"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <property name="packagesToScan" value="com.mypackage2.Model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
</bean> 

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

<bean id="dataSource2"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:database/~test2" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>     

我无法为此找到任何合适的配置。

对此表示感谢。

0 个答案:

没有答案