Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no magic under the hood.</p> <pre><code>&lt;jpa:repositories base-package="com.foo.repos.ora" entity-manager-factory-ref="entityManagerFactory"/&gt; </code></pre> <p>doesn't help you at all with your custom interface implementations. Best way I found is to treat your custom implementations as regular beans. So I defined a 'sharedEntitManager' bean in my spring configuration like so</p> <pre><code>&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; ... &lt;/bean&gt; &lt;bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt; &lt;property name = "entityManagerFactory" ref="entityManagerFactory"/&gt; &lt;/bean&gt; </code></pre> <p>After that, I simply injected the EntityManager into my implementation beans</p> <pre><code>&lt;bean id="aRepositoryImpl" class="comm.foo.repos.ora.ARepositoryImpl"&gt; &lt;property name="entityManager" ref="sharedEntityManager"/&gt; &lt;/bean&gt; </code></pre> <p>The 'entity-manager-factory-ref' attribute discriminates between different entitymanager factories but only for straight Spring Data Repositories (i.e. only for interfaces). It doesn't however concern itself with any of your implementations.</p> <p>To sum it up</p> <p>1) if you simply rely on standard Spring Data repositories with no custom implementation, use the "entity-manager-factory-ref" attribute to differentiate databases.</p> <p>2a) Additionally, if you use any custom implementation, inject the appropriate EntityManager directly into the implementing class. Wirering is done under control of your spring xml configuration. For some reason I wasn't able to use the @Autowire annotation with a @Qualifier to reference the correct EntityManager. <strong>EDIT</strong> I just learned about the <a href="http://forum.springsource.org/showthread.php?60659-Problem-with-Autowired-%28requires-Qualifier%29&amp;p=203095#post203095">@Resource</a> annotation</p> <pre><code>@Resource(name = "sharedEntityManagerA") EntityManager entityManager &lt;bean id="sharedEntityManagerA" name="sharedEntityManagerA" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt; &lt;property name = "entityManagerFactory" ref="entityManagerFactory"/&gt; &lt;/bean&gt; </code></pre> <p>With this at hand selecting what EntityMAnger should be used becomes straightforward. No need of plumbing everything togehther in your context xml.</p> <p>2b) As an alternative to Spring's xml configuration for hooking up your stuff you may also go with </p> <blockquote> <p>@PersistenceContext( unitName = "nameOfPersistenceUnit" )</p> </blockquote> <p>to inject the correct EntitymanagerFactory</p> <p>While 'nameOfPersistenceUnit' referes to your persistence sitting in your standard JPA persistence.xml</p> <p>However 2b) doesn't go well with 'QueryDslRepositorySupport', since it expects an EntityManager instance. But I found that 'QueryDslRepositorySupport' doesn't offer much support anyway, so I removed it.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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