question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to inject JPA repositories for different datasources

See original GitHub issue

I have an application that needs to use the same JPA repository on different datasources. At first I tried to inject it with @PersistenceContext(name = "<datasource>") like it works for a transaction manager but without success. I just found out that the @Repository annotation can be used with a datasource name. But in my opinion that is the wrong place for this declaration. It requires to define multiple repository interfaces which have exactly the same structure. Possibly that could be solved with inheritance (haven’t tried) but it still contradicts the logical structure as that would be a declaration just for runtime purposes. It also makes it difficult to define repositories in libraries.

Wouldn’t it be possible to use the @PersistenceContext annotation for repository injection too?

Expected Behaviour

Inject JPA repository variables for different (non-default) datasources.

Actual Behaviour

The datasource can only be controlled by @Repository annotation, requiring to define multiple identical repository interfaces.

Environment Information

  • Micronaut Version: 1.3.0.M2
  • micronaut-data Version: 1.0.0.M5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dogcowcommented, Jul 1, 2022

I could flesh out my example code above.

0reactions
dstepanovcommented, Jul 4, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring JPA – Multiple Databases - Baeldung
First, let's create two simple entities, with each living in a separate database. Here is the first User entity: package com.baeldung.multipledb ...
Read more >
How to inject a JPA repository for multiple datasources in ...
I would like to inject them into different variable, e.g. sourceRepo and targetRepo . The datasources are declared in the application.yml with ...
Read more >
Configuring multiple data sources with Spring Boot 2 and ...
To configure multiple data sources in Spring Data JPA we need to group the Model classes and Repository interfaces for each data source...
Read more >
2. JPA Repositories - Spring
Explicitly wire the EntityManagerFactory to be used with the repositories being detected by the repositories element. Usually used if multiple ...
Read more >
Connect Multiple JPA repositories using Static and Dynamic ...
The JPA Repository is a useful Spring Framework library that provides object-relational mapping for Java web applications to be able to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found