2011-05-23 49 views
0

好的,我對此很感興趣。我想要做的是說「這些類在這裏(數據庫a)和這些類(數據庫b)」。我認爲我應該明確地定義在不同的持久性單位組下面的類,它們也可以用驅動程序信息保存屬性的集合。在Spring中爲持久化單元使用不同的數據源

<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> 

然後在我的道班,我應該只提供上下文:

@Repository 
public class AFCARSJpaDao 
{ 
    @PersistenceContext(unitName = "nytdModel") 
    private EntityManager entityManger; 
} 

不過,我得到一個No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2錯誤。我究竟做錯了什麼?

我使用Spring 3.0.4

回答

2

看起來你嘗試注入與@AutowiredEntityManagerFactory地方。

始終使用@PersistenceContext注入EntityManager@PersistenceUnit注入EntityManagerFactory,應正確處理多個持久化單元的情況下(如果你對他們的指定unitName屬性)。

+0

true也非常重要。否則,它會一直告訴你,一張桌子不會因爲一個不同的persitence unit的實體而退出。在我看到這個問題之前給我一個真正的痛苦。多謝你們!!! – Franklin 2011-07-22 19:54:22

相關問題