Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ease access to the EntityManager for a given domain type from custom implementations [DATAJPA-669] #1047

Closed
spring-projects-issues opened this issue Feb 2, 2015 · 6 comments
Assignees
Labels
in: core Issues in core support type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Feb 2, 2015

Mauro Molinari opened DATAJPA-669 and commented

My repositories are defined in a reusable service layer and may be imported and used by different applications. Those applications may as well define more than one EntityManagerFactory, but only one will be supplied to <jpa:repositories> definition in the Spring application context to configure my repositories.

The problem is that when I write my custom implementations I may need to get a reference to that EntityManagerFactory, but I couldn't find a way to cleanly do this. In fact, I would need to hard-code in that custom implementation the name of the persistence unit of the EMF I want to receive, in order to get autowiring work correctly. But this is not optimal, of course, because different applications may inject an EMF with a different persistence unit name into my repositories.

The best I could do is to set up a convention: my repository service layer expects that there's an EMF named myConventionalPU available: every application that uses it, must ensure that the EMF injected into Spring Data is also aliased by that conventional name. But it's of course a weak requirement, hard to enforce.

What I would need, in fact, is a way to get information from Spring Data within my custom implementations. Honestly, the fact that the Impl 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 (the EMF, for instance, or any other standard method of the repository you're customizing. It happens almost always for me.)


Affects: 1.7.1 (Evans SR1)

Reference URL: http://stackoverflow.com/questions/27606905/how-to-access-spring-data-configured-entity-manager-factory-in-custom-implemen

Issue Links:

  • DATAJPA-779 Update reference documentation to mention newly introduced JpaContext

Referenced from: commits 5f1ab56

1 votes, 3 watchers

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Apr 13, 2015

Thierry RAMANAMPANOHARANA commented

I wrote a custom repository for the sole purpose of getting the entity manager. And you are right, when you have multiple persistence units, getting the entity manager from the repository relieves you from specifying which persistence unit you want the EM from as the repository is tied to the entity which is tied to the persistence unit.

We use it for example when working with more advanced features of QueryDsl, like fetch join with deeper level or batch update using criteria. Now that we have entity graph and batch update in JPA 2.1 we may not want that anymore. Anyway, we found QueryDsl to be powerful in that regard.

We extend the implementation of the custom repository from QueryDslJpaRepository and you can get access to the entity manager from the constructor. You just can store that in an instance variable.

It would be nice if the standard repository exposes the entity manager right away

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Aug 17, 2015

Mauro Molinari commented

Hi, I see this is fixed, thank you!! Any pointer to the updated documentation where it's explained how to get the injected Entity Manager [Factory]?

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Aug 18, 2015

Oliver Drotbohm commented

Probably something worth mentioning in there reference documentation, but the basic idea is that you'd get a JpaContext injected into the repository which allows you to obtain the EntityManager per managed domain type

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Aug 19, 2015

Mauro Molinari commented

Ok, thank you Oliver!

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Aug 19, 2015

Oliver Drotbohm commented

I created DATAJPA-779 to keep track of the documentation efforts

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jan 20, 2016

Mauro Molinari commented

Oliver, I just looked at the implementation of org.springframework.data.jpa.repository.support.DefaultJpaContext.getEntityManagerByManagedType(Class<?>) and I see the case in which an entity class might be managed by more than one entity manager.
But, just for curiosity, why not just providing a getEntityManager() method in JpaRepository to return the entity manager used by the repository you're customizing? In that case, the custom implementation MyEntityRepositoryCustom may simply need to get the MyEntityRepository instance injected and call getEntityManager() on it to get the entity manager in use (without any risk of ambiguity)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants