Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-14020

Enhance @EntityScan to work for multiple EntityManagerFactories

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Invalid
    • Affects Version/s: 4.2.4
    • Fix Version/s: None
    • Component/s: Data:ORM
    • Labels:
    • Last commented by a User:
      true

      Description

      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.

      MyDatasourceConfig.java
      @Configuration
      @EnableDataSource(name = "red", prefix = "my.red", primary = true)
      @EnableDataSource(name = "blue", prefix = "my.blue")
      
      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.

      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.

        Attachments

          Activity

            People

            Assignee:
            snicoll St├ęphane Nicoll
            Reporter:
            andyRokit Andrew Neeson
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 31 weeks, 3 days ago