3
votes

I am writing a Spring Boot application which has multiple dataSources and entityManagers and I want to use the JPA CrudRepository interface like:

@Repository public interface Car extends CrudRepository<Car.class, Long> {}.

I get the following error:

Error creating bean with name 'carRepository': Cannot create inner bean '(inner bean)#350d0774' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#350d0774': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource

I have not been able to figure out how to tell the JpaRepositoryFactory which entity manager to use when building the repositories. It defaults to trying to inject an EntityManagerFactory called: entityManagerFactory

2
What does your applicationContext.xml or Java Configuration look like; have you created an EntityManagerFactory Bean in the configuration? Here is the documentation.William Callahan

2 Answers

5
votes

Check this documentation on attributes.

Use the @EnableJpaRepositories annotation below on your Java configuration class with entityManagerFactoryRef and transactionManagerRef attributes.

@EnableJpaRepositories(basePackages = {
        "com.myapp.repositories" }, entityManagerFactoryRef = "entityManagerFactoryRef1", transactionManagerRef = "transactionManagerRef1")

Remove @Repository annotation on Car interface. Spring will register it as JPA repository if you specify basePackages attribute.

3
votes

what you can do in your spring.xml

    <jpa:repositories base-package="com.a.b.repository"
    entity-manager-factory-ref="yrEntityMAnagerFectorybeanRef"
    transaction-manager-ref="yourTransactionManagerBeanRef"/>

in your EntityManagerFactory bean you will refer your entitymanager bean.