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

Closed
spring-projects-issues opened this issue Mar 4, 2016 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues 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.

E.g.
{code: title=MyDatasourceConfig.java}
@Configuration
@EnableDataSource(name = "red", prefix = "my.red", primary = true)
@EnableDataSource(name = "blue", prefix = "my.blue")


{code: title=application.properties}
my.red.spring.datasource.username=red_database_user
my.red.spring.jpa.hibernate.ddl-auto=create
my.red.spring.jpa.database=sql-server
...
my.blue.spring.datasource.username=blue_database_user
my.blue.spring.jpa.hibernate.ddl-auto=validate
my.blue.spring.jpa.database=oracle
...

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.

{code: title=MyAppRedConfig.java}
@Configuration
@EnableJpaRepositories(
basePackages = { "my.red.data.access" },
entityManagerFactoryRef = "redEntityManagerFactory",
transactionManagerRef = "redTransactionManager"
)

@EntityScan(
basePackages = { "my.red.data.model" },
entityManagerFactoryRef = "redEntityManagerFactory"
)


..it'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

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues 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?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues 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

@spring-projects-issues spring-projects-issues added status: invalid An issue that we don't feel is valid in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: enhancement A general enhancement label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants