Question

I am playing with spring-boot and spring-boot-data-jpa. I need that @Entity class A {...} uses hsqlDatasource and @Entity class B {...} use mssqlDatasource. How can I achieve that?

I would like to do something like this:

@Repository
@UseDatasource("hsql")
interface A extends CrudRepository....
Was it helpful?

Solution

Spring Data supports this pattern through @EnableJpaRepositories(entityManagerFactoryRef=...). To make it work with Spring Boot you could create your 2 DataSources, mark one of them @Primary and it will be used by the auto-configured entityManagerFactory bean. Then you add another and refer to them both individually in your 2 @EnableJpaRepositories annotations.

I suppose you could even create a custom annotation for each of the two repository types and use it in @EnableJpaRepositories(includeFilters=...), but if I were you I would get it working with a package scan first (i.e. just put the repositries for the 2 data source targets in different packages).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top