java - List of CrudRepository items -


i need make query number of databases. number of databases can vary.

it looks convinient have ability iterate them through list.

is idea, or @ least possible have list of repositories connected different datasources or there better way?

i've read how use 2 or more databases spring? . implementing have know number of databases, can vary.

i tried:

@entity public class member{}  public interface memberrepository extends crudrepository<member, long> { }  @component @configurationproperties("myclass") public class myclass {     @autowired     private memberrepository memberrepository; }  @component @configurationproperties("myclasshub") public class myclasshub {     @autowired     private list<myclass> myclasses; } 

this looks logical me. cannot understand how configure list items. such configuration not work.

application.yml

myclasshub:   myclasses:     -       spring:         datasource:           url: jdbc:mysql://url           username: username           password: password     -       spring:         datasource:           url: jdbc:mysql://url           username: username           password: password 

in pom.xml have:

    <dependency>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-data-jpa</artifactid>     </dependency>      <dependency>         <groupid>mysql</groupid>         <artifactid>mysql-connector-java</artifactid>     </dependency> 

i thought using abstractroutingdatasource, architecture of classes become complicated , not logical.

finally found solution. idea use simplejparepository instead of crudrepository. bit tricky.

my example:

pom.xml:

<dependency>     <groupid>org.springframework.boot</groupid>     <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency>     <groupid>mysql</groupid>     <artifactid>mysql-connector-java</artifactid> </dependency> 

you need configure unit in persistence.xml transaction-type="resource_local"

persistence.xml:

<?xml version="1.0" encoding="utf-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">     <persistence-unit name="my-jpa" transaction-type="resource_local">     </persistence-unit> </persistence> 

then need map properties(url, driver, user, password...) can them @autowired or fill in runtime:

map dbproperties; dbproperties = new hashmap(); dbproperties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.driver"); dbproperties.put("javax.persistence.jdbc.url", "jdbc:mysql://url/database"); dbproperties.put("javax.persistence.jdbc.user", "user"); dbproperties.put("javax.persistence.jdbc.password", "password"); 

entitymanager , entitymanagerfactory:

entitymanagerfactory emf = persistence.createentitymanagerfactory("my-jpa", dbproperties); entitymanager entitymanager = emf.createentitymanager(); 

and finally:

jparepository = new simplejparepository<member, long>( member.class, entitymanager); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -