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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

The <exclude-unlisted-classes>true</exclude-unlisted-classes> 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!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.