Reputation: 1149
I'm trying to get Spring Boot running with multiple databases
I've found How to use 2 or more databases with spring? and http://xantorohara.blogspot.com.au/2013/11/spring-boot-jdbc-with-multiple.html which have been helpfully. However, the class TomcatDataSourceConfiguration isn't included in version 1.2.4. How can I get this working using the newer versions?
EDIT: Now I'm getting the error No bean named 'entityManagerFactory' is defined
DatabaseConfiguration.java
@Configuration
@EnableAutoConfiguration
public class DatabaseConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.weather")
public DataSource weatherDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean weatherManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(weatherDataSource())
.packages(User1.class)
.persistenceUnit("user2")
.build();
}
@Bean
@ConfigurationProperties(prefix = "spring.weather_alerts")
public DataSource weatherAlertsDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean weatherAlertsManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(weatherAlertsDataSource())
.packages(User1.class)
.persistenceUnit("user1")
.build();
}
}
User1.java
@Entity
@Table(name = "users1")
public class User1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name1;
public User1() {}
public User1(String name1) {
this.name1 = name1;
}
// Getter Setters
}
User2.java
@Entity
@Table(name = "users2")
public class User2 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name1;
public User2() {}
public User1(String name1) {
this.name2 = name2;
}
// Getter Setters
}
User1Dao.java
@Transactional
public interface User1Dao extends CrudRepository<User1, Long> {
}
User2Dao.java
@Transactional
public interface User2Dao extends CrudRepository<User2, Long> {
}
UserController.java
@Controller
public class UserController implements CommandLineRunner {
@Autowired private User1Dao user1Dao;
@Autowired private User2Dao user2Dao;
@Override
public void run(String... arg0) throws Exception {
user1Dao.save(new User1("name 1"));
user2Dao.save(new User2("name 2"));
}
}
Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
spring.weather_alerts.url=jdbc:mysql://localhost:3306/weather_alerts
spring.weather_alerts.username=root
spring.weather_alerts.password=
spring.weather.url=jdbc:mysql://localhost:3306/weather
spring.weather.username=root
spring.weather.password=
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Upvotes: 1
Views: 3885
Reputation: 72
Spring Data by default looks for an EntityManagerFactory
bean named "entityManagerFactory", see the docs. Since you don't have an EntityManagerFactory
bean by that name, you're seeing the error.
The solution is to define the entityManagerFactoryRef
name in your config, e.g.
@EnableJpaRepositories(entityManagerFactoryRef = "weatherManagerFactory")
You may need to create two separate config classes, each containing one of your LocalContainerEntityManagerFactoryBean
beans, and have a class-level @EnableJpaRepositories
for each.
Side note, Don't forget to exclude the DataSourceAutoconfiguration
class in your Application.java
since you're configuring the datasources yourself.
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
Upvotes: 1