我正在尝试将两个不同EntityManagerFactory
的实例注入同一个 DAO。我在一个项目中有一个通用的 DAO,我们称之为“项目库”,我有两个不同的项目,项目 A 和项目 B。持久单元在项目库中定义。
我的问题是我希望能够将EntityManagerFactory
实例注入 DAO 而不是使用@PersistenceUnit
,因为我有两个不同的数据库,一个由项目 A 使用,另一个由项目 B 使用。
我在通用 DAO 中只有基本的 CRUD 操作,但 Spring 给了我以下异常:
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>
我一直在努力解决这个问题。我会很感激任何帮助。
提前致谢!