开发者

Using different datasources for persistence units in Spring

开发者 https://www.devze.com 2023-03-08 06:52 出处:网络
Ok, I\' new to this.What I want to do is say \"these classes are persisted over here (database a), and these classes over there (database b)\".I think I\'m supposed to define the classes explicitly un

Ok, I' new to this. What I want to do is say "these classes are persisted over here (database a), and these classes over there (database b)". I think I'm supposed to define the classes explicitly under different persistence-unit groups, which can also hold a collection of properties with the driver info.

<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>
开发者_Go百科

Then in my Dao classes, I should just provide the context:

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

However, I'm getting a No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2 error. What am I doing wrong?

I'm using Spring 3.0.4


It looks like you try to inject an EntityManagerFactory with @Autowired somewhere.

Always use @PersistenceContext to inject EntityManager and @PersistenceUnit to inject EntityManagerFactory, they should handle the case of multiple persistence units correctly (if you specify unitName attribute on them).

0

精彩评论

暂无评论...
验证码 换一张
取 消