Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Data JPA: Repositories for multiple database / Entitymanger configurations
    primarykey
    data
    text
    <p>I have two <code>Entitymanager</code> bean configurations. Each pointing to a separate database with a different schema (one is Oracle, the other one is an in-memory H2)</p> <p>What could I do to solve the ambiguity of what Entitymanager should be used for each Repository? Right now I'm getting this error:</p> <pre><code> No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2 </code></pre> <p>I guess I could provide a quick-fix simply by using something like</p> <pre><code>&lt;jpa:repositories base-package="com.foo.repos.ora" entity-manager-factory-ref="entityManagerFactoryA"&gt; &lt;jpa:repositories base-package="com.foo.repos.m2" entity-manager-factory-ref="entityManagerFactoryB"&gt; </code></pre> <p>But hopefully there is a better solution.</p> <p><strong>EDIT:</strong></p> <p>I give you an idea of the current scenario:</p> <p>Spring-Config: there're two EM</p> <pre><code>&lt;jpa:repositories base-package="com.foo.repos.ora" entity-manager-factory-ref="entityManagerFactory"/&gt; &lt;jpa:repositories base-package="com.foo.repos.m2" entity-manager-factory-ref="entityManagerFactory2"/&gt; &lt;context:component-scan base-package="com.foo" /&gt; .... </code></pre> <p>Everything from here on is in "package com.foo.repos.ora" Following the pattern of <a href="https://stackoverflow.com/questions/11880924/how-to-add-custom-method-to-spring-data-jpa">how to make a custom repository</a> I get two interfaces 'ARepository', 'ARepositoryCustom' and its implementation 'ARepositoryImpl' like so</p> <pre><code>@Repository public interface ARepository extends ARepositoryCustom, JpaRepository&lt;myEntity, BigDecimal&gt;, QueryDslPredicateExecutor { } public interface ARepositoryCustom { FooBar lookupFooBar() } public class ARepositoryImpl extends QueryDslRepositorySupport implements ARepositoryCustom { ARepositoryImpl(Class&lt;?&gt; domainClass) { super(domainClass.class) } ARepositoryImpl() { this(myEntity.class) } @Override FooBar lookupFooBar() { JPQLQuery query = .... .... return found } } </code></pre> <p>resulting in the following error message:</p> <blockquote> <p>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '<strong>aRepositoryImpl</strong>': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: <strong>expected single bean but found 2</strong></p> </blockquote> <p>Which is of course correct, there are 2 EM beans, but since I restricted EM #1 aka 'entityManagerFactory' to package 'com.foo.repos.ora' only, I'm still not sure how to reference the exact EM bean.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload