title: How to access Spring Data configured entity manager (factory) in custom implementation
body: <p>I'm writing a custom implementation for a Spring Data JPA repository. So I have:</p>
  
  <ul>
  <li><code>MyEntityRepositoryCustom</code> => interface with the custom methods</li>
  <li><code>MyEntityRepositoryUmpl</code> => implementation of the above interface</li>
  <li><code>MyEntityRepository</code> => standard interface which extends <code>JpaRepository</code> and <code>MyEntityRepositoryCustom</code></li>
  </ul>
  
  <p>My problem is this: within the implementation of <code>MyEntityRepositoryUmpl</code> I need to access the entity manager that was injected into Spring Data. How to get it?
  I can use <code>@PersistenceContext</code> to get it autowired, but the problem is that this repository must work in an application that sets up more than one persistence units. So, to tell Spring which one I need, I would have to use <code>@PersistenceContext(unitName="myUnit")</code>. However, since my repositories are defined in a reusable service layer, I can't know at that point what will be the name of the persistence unit that the higher-level application layer will configure to be injected into my repositories.</p>
  
  <p>In other words, what I would need to do is to access the entity manager that Spring Data itself is using, but after a (not so quick) look at Spring Data JPA documentation I couldn't find anything for this.</p>
  
  <p>Honestly, the fact that the <code>Impl</code> classes are totally unaware of Spring Data, although described as a strength in Spring Data manual, is actually a complication whenever you need to access something that is usually provided by Spring Data itself in your custom implementation (almost always, I would say...).</p>