1

I have a micronaut-data application and for a copy operation I need to use the same JPA repository to access multiple datasources. I would like to inject them into different variable, e.g. sourceRepo and targetRepo. The datasources are declared in the application.yml with the names default and target. If I declare a repository variable with @Inject it will be initialized to access the default datasource.

The question is, how can I declare an injected repository variable so that it will access the target datasource? I can declare target injections of EntityManager or SynchronousTransactionManager like below and these work. But neither of these annotations has an effect on a repository variable or parameter.

    @Inject
    @CurrentSession("target")
    EntityManager entityManager;

    @PersistenceContext(name = "target")
    SynchronousTransactionManager<Connection> transactionManager;    
eso
  • 78
  • 1
  • 9

1 Answers1

1

To answer my own question: currently (as of micronaut-data 1.0.0.RC1) injecting repositories directly for different datasources is not possible. Instead, the @Repository annotation optionally accepts a datasource name (e.g. @Repository("target"). This means that to use a repository on different repositories, it needs to be declared in a specific class. To use such a repository an instance of the respective class needs to be injected into the using code.

Changing this has been proposed in the micronaut-data project and is under consideration.

eso
  • 78
  • 1
  • 9