0

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>

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

end-user
  • 2,845
  • 6
  • 30
  • 56

1 Answers1

2

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

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • The true is also very important. Or else it will keep telling you that a table doesnt exit for an enitity of a different persitence unit.Was giving me a real pain till I saw this question. Thanks guys!!! – Franklin Jul 22 '11 at 19:54