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

Enhance @EntityScan to work for multiple EntityManagerFactories [SPR-14020] #18592

spring-issuemaster opened this issue Mar 4, 2016 · 2 comments


Copy link

@spring-issuemaster spring-issuemaster commented Mar 4, 2016

Andrew Neeson opened SPR-14020 and commented

Spring Boot makes it really easy to include a single, default data source. It auto-configures all the required components (TransactionManager, EntityManagerFactory, JpaVendorAdapter, Datasource...).

The existing @EntityScan is great in that it it allows you to effectively loosely-couple your persistence implementation. Without it, you'd need to hard-code your packagesToScan.

My issue comes when you want to use multiple datasources. It's not that hard to create the beans manually. However, it does have one unfortunate side-effect; it means that we can't use @EntityScan. So we're back to hard-coding the packagesToScan parameter.

Would it be possible to do the following:

Add an annotation for auto-configuring multiple data sources
The annotation should trigger behaviour exactly like Spring Boot does with a single data source, but for multiple data sources.

@EnableDataSource(name = "red", prefix = "", primary = true)
@EnableDataSource(name = "blue", prefix = "")


Would create the following beans:

  • redTransactionManager
  • redEntityManagerFactory
  • redDatasource
  • blueTransactionManager
  • blueEntityManagerFactory
  • blueDatasource

The @EntityScan could then be enhanced to only configure the primary EntityManager (which should help make this change backwards-compatible with existing code).

An additional parameter: "entityManagerFactoryRef" could be added to specify which EntityManagerFactory to use. E.g.

basePackages = { "" },
entityManagerFactoryRef = "redEntityManagerFactory",
transactionManagerRef = "redTransactionManager"

basePackages = { "" },
entityManagerFactoryRef = "redEntityManagerFactory"
)'d also be nice if @EnableJpaRepositories was @Repeatable so we don't have to create separate config files for the red and blue datasources, but it's a minor issue.

Affects: 4.2.4


This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 14, 2016

Stéphane Nicoll commented

That's certainly a Spring Boot issue, not a Spring Framework issue. Can you please report that issue in the Spring Boot issue tracker?


This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 14, 2016

Andrew Neeson commented

Sorry - I didn't realise that Spring Boot was managed outside this site.

I've opened spring-projects/spring-boot#5401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.