Hibernate 在Spring中为持久化单元使用不同的数据源
好的,我对这个还不熟悉。我想说的是“这些类在这里(数据库a)被持久化,而这些类在那里(数据库b)”。我想我应该在不同的持久化单元组下显式地定义类,这些持久化单元组还可以包含一个包含驱动程序信息的属性集合Hibernate 在Spring中为持久化单元使用不同的数据源,hibernate,spring,jpa,persistence-unit,Hibernate,Spring,Jpa,Persistence Unit,好的,我对这个还不熟悉。我想说的是“这些类在这里(数据库a)被持久化,而这些类在那里(数据库b)”。我想我应该在不同的持久化单元组下显式地定义类,这些持久化单元组还可以包含一个包含驱动程序信息的属性集合 <persistence-unit name="nytdModel" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider>
<persistence-unit name="nytdModel" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>gov.vermont.dcf.nytd.model.AbstractElementImpl</class>
...
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://localhost;..."/>
<property name="hibernate.connection.username" value="..."/>
<property name="hibernate.connection.password" value="..."/>
</properties>
</persistence-unit>
然而,我得到了一个没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:预期为单个bean,但发现了2个错误。我做错了什么
我使用的是Spring 3.0.4,看起来您试图在某个地方使用@Autowired
注入EntityManagerFactory
始终使用@PersistenceContext
来注入EntityManager
和@PersistenceUnit
来注入EntityManagerFactory
,它们应该正确处理多个持久性单元的情况(如果您在它们上指定unitName
属性)。真实情况也非常重要。否则它会一直告诉你,一张桌子不会因为一个不同的持久性单位而退出。在我看到这个问题之前,这让我非常痛苦。谢谢大家!!!
@Repository
public class AFCARSJpaDao
{
@PersistenceContext(unitName = "nytdModel")
private EntityManager entityManger;
}