我正在尝试将两个不同的EntityManagerFactory
实例注入同一个DAO。
我在项目中有一个通用DAO,我们称之为“项目基础”,我有两个不同的项目,项目A和项目B.持久单位在项目库中定义。
我的问题是我希望能够将EntityManagerFactory
实例注入DAO而不是使用@PersistenceUnit
,因为我有两个不同的数据库,一个由项目A使用,另一个由Project使用乙
我只在通用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>
此文件位于Project Base中:
<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>
我一直在努力想出这个。我很感激任何帮助。
提前致谢!
答案 0 :(得分:1)
@PersistenceUnit(unitName="A")
EntityManagerFactory entityMgrFactory;
OR(这将注入与persistenceUnit A相关的EMFactory)
@Autowired
@Qualified("AEntityManagerFactory")
private EntityManagerFactory AEntityManagerFactory;
OR
@PersistenceContext(unitName="A")
EntityManager em;
如果没有unitName总是希望只有一个EMFactory而且你有两个,那么spring不知道哪个传递。
答案 1 :(得分:0)
您是否尝试使用@PersistenceUnit.unitName
配置持久性单元?