'How to use Multiple JPA repositories that uses different Databases?

I have two JPA repositories. One Repository contains Postgres queries and the other one contain Oracle queries. They share the same function declaration, only the @Query(...) content is different(specific to Database). It's like, Oracle DB is used in some environments, and Postgres is been used in other environments.

Structure:

public interface OracleQuery extends JpaRepository<T, ID> {

   @Query("oracle query....")
   User getData();
}

public interface PostgresQuery extends JpaRepository<T, ID> {

   @Query("postgres query....")
   User getData();
}

Now,

@Autowired
OracleQuery oracle;

@Autowired
PostgresQuery postgres; 

Instead, my question is, Is it possible to create a Generic Type which will decide during the runtime which Repository to look into based on the configuration or the profile provided?

Something like,

@Autowired
GenericType<?> oracle_postgres; // Based on the profile/ application configs, figures out which Repository to look into

The question might sound stupid, but is it possible? Since I am new to Java programming and Spring Framework, I am getting to understand the topics.



Solution 1:[1]

try using different spring profiles for your envs. Then you can use @Profile annotation like e.g.

@Component
@Profile("a & !b")
public class UsedInProfileAAndNotProfileB {}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Stroebel