將不同的實體管理器工廠注入到相同的DAO中我試圖將兩個不同的EntityManagerFactory
實例注入到同一個DAO中。 我在一個項目中有一個通用的DAO,我們稱之爲「Project Base」,並且我有兩個不同的項目,即項目A和項目B.持久單元在Project Base中定義。如何使用spring + JPA
我的問題是,我希望能夠給EntityManagerFactory
實例注入到DAO,而不是使用@PersistenceUnit
因爲我有兩個不同的數據庫,其中一個是被項目A和另一個由項目B.
我的只有在通用DAO的基本CRUD操作,但春天給了我以下異常:
Error creating bean with name 'genericHibernateDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
請在下面找到我的配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="autodetect">
<context:component-scan base-package="com.entertainment" />
<!-- Entity Manager -->
<bean id="abstractEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" abstract="true">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="dataSource" ref="ADataSource" />
</bean>
<bean id="AEntityManagerFactory" parent="abstractEntityManagerFactory">
<property name="persistenceUnitName" value="A" />
</bean>
<bean id="BEntityManagerFactory" parent="abstractEntityManagerFactory">
<property name="persistenceUnitName" value="B" />
</bean>
<!-- Datasource -->
<bean id="ADataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="myUrl" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="BDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="myUrl" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<!-- Transaction Manager -->
<bean id="ATransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="BTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Vendor adapter -->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle9iDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<!-- generic DAO -->
<bean id="genericDAO"
class="mypackage.GenericHibernateDAO" abstract="true"/>
</beans>
此文件是在項目基地:
<persistence 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_2_0.xsd"
version="2.0">
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="A" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url" value="myurl" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
<persistence-unit name="B" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url" value="myurl" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
我一直真的努力摸不着頭腦。我會很感激任何幫助。
在此先感謝!