forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5323: Merge sources into existing persistence module (eugenp#11804)
* BAEL-5323: Spring Boot with multiple data sources * BAEL-5323: small fixes * BAEL-5323: fix test class * BAEL-5323: Refactor configuration classes * BAEL-5323: Set proeprty to private * BAEL-5323: rename test class to follow naming conventions * BAEL-5323: merge sources into existing persistence module * BAEL-5323: merge sources into existing persistence module
- Loading branch information
Showing
19 changed files
with
324 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...odules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/entity/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...es/spring-data-jdbc/src/main/java/com/baeldung/springmultipledatasources/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.springmultipledatasources; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.core.env.AbstractEnvironment; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "multipledatasources"); | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...les/spring-data-jdbc/src/main/java/com/baeldung/springmultipledatasources/todos/Todo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.baeldung.springmultipledatasources.todos; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Todo { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
private String title; | ||
private boolean completed; | ||
|
||
public Todo() { | ||
} | ||
|
||
public Todo(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public boolean isCompleted() { | ||
return completed; | ||
} | ||
|
||
public void setCompleted(boolean completed) { | ||
this.completed = completed; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...c/main/java/com/baeldung/springmultipledatasources/todos/TodoDatasourceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.springmultipledatasources.todos; | ||
|
||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
public class TodoDatasourceConfiguration { | ||
|
||
@Bean | ||
@ConfigurationProperties("spring.datasource.todos") | ||
public DataSourceProperties todosDataSourceProperties() { | ||
return new DataSourceProperties(); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource todosDataSource() { | ||
return todosDataSourceProperties() | ||
.initializeDataSourceBuilder() | ||
.build(); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...jdbc/src/main/java/com/baeldung/springmultipledatasources/todos/TodoJpaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung.springmultipledatasources.todos; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
basePackageClasses = Todo.class, | ||
entityManagerFactoryRef = "todosEntityManagerFactory", | ||
transactionManagerRef = "todosTransactionManager" | ||
) | ||
public class TodoJpaConfiguration { | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean todosEntityManagerFactory( | ||
@Qualifier("todosDataSource") DataSource dataSource, | ||
EntityManagerFactoryBuilder builder) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages(Todo.class) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager todosTransactionManager( | ||
@Qualifier("todosEntityManagerFactory") LocalContainerEntityManagerFactoryBean todosEntityManagerFactory) { | ||
return new JpaTransactionManager(Objects.requireNonNull(todosEntityManagerFactory.getObject())); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...-data-jdbc/src/main/java/com/baeldung/springmultipledatasources/todos/TodoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.springmultipledatasources.todos; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TodoRepository extends JpaRepository<Todo, Long> { | ||
} |
39 changes: 39 additions & 0 deletions
39
...s/spring-data-jdbc/src/main/java/com/baeldung/springmultipledatasources/topics/Topic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.baeldung.springmultipledatasources.topics; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Topic { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
private String title; | ||
|
||
public Topic() { | ||
} | ||
|
||
public Topic(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...main/java/com/baeldung/springmultipledatasources/topics/TopicDatasourceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baeldung.springmultipledatasources.topics; | ||
|
||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
public class TopicDatasourceConfiguration { | ||
|
||
@Bean | ||
@ConfigurationProperties("spring.datasource.topics") | ||
public DataSourceProperties topicsDataSourceProperties() { | ||
return new DataSourceProperties(); | ||
} | ||
|
||
@Bean | ||
public DataSource topicsDataSource() { | ||
return topicsDataSourceProperties() | ||
.initializeDataSourceBuilder() | ||
.build(); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...bc/src/main/java/com/baeldung/springmultipledatasources/topics/TopicJpaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baeldung.springmultipledatasources.topics; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
basePackageClasses = Topic.class, | ||
entityManagerFactoryRef = "topicsEntityManagerFactory", | ||
transactionManagerRef = "topicsTransactionManager" | ||
) | ||
public class TopicJpaConfiguration { | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory( | ||
@Qualifier("topicsDataSource") DataSource dataSource, | ||
EntityManagerFactoryBuilder builder | ||
) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages(Topic.class) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager topicsTransactionManager( | ||
@Qualifier("topicsEntityManagerFactory") LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory) { | ||
return new JpaTransactionManager(Objects.requireNonNull(topicsEntityManagerFactory.getObject())); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ata-jdbc/src/main/java/com/baeldung/springmultipledatasources/topics/TopicRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.springmultipledatasources.topics; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TopicRepository extends JpaRepository<Topic, Long> { | ||
} |
6 changes: 6 additions & 0 deletions
6
persistence-modules/spring-data-jdbc/src/main/resources/application-jdbcintro.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#H2 DB | ||
spring.jpa.hibernate.ddl-auto=none | ||
spring.datasource.url=jdbc:h2:mem:persondb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password=test |
9 changes: 9 additions & 0 deletions
9
...ce-modules/spring-data-jdbc/src/main/resources/application-multipledatasources.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
spring.jpa.hibernate.ddl-auto=update | ||
spring.datasource.todos.url=jdbc:h2:mem:todos | ||
spring.datasource.todos.username=sa | ||
spring.datasource.todos.password=null | ||
spring.datasource.todos.driverClassName=org.h2.Driver | ||
spring.datasource.topics.url=jdbc:h2:mem:topics | ||
spring.datasource.topics.username=sa | ||
spring.datasource.topics.password=null | ||
spring.datasource.topics.driverClassName=org.h2.Driver |
7 changes: 1 addition & 6 deletions
7
persistence-modules/spring-data-jdbc/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
#H2 DB | ||
spring.jpa.hibernate.ddl-auto=none | ||
spring.h2.console.enabled=true | ||
spring.datasource.url=jdbc:h2:mem:persondb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password=test | ||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect | ||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect |
41 changes: 41 additions & 0 deletions
41
.../test/java/com/baeldung/springmultipledatasources/MultipleDatasourcesIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baeldung.springmultipledatasources; | ||
|
||
import com.baeldung.springmultipledatasources.todos.Todo; | ||
import com.baeldung.springmultipledatasources.todos.TodoRepository; | ||
import com.baeldung.springmultipledatasources.topics.Topic; | ||
import com.baeldung.springmultipledatasources.topics.TopicRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ActiveProfiles("multipledatasources") | ||
@DataJpaTest // no test database! | ||
class MultipleDatasourcesIntegrationTest { | ||
|
||
@Autowired | ||
TodoRepository todoRepo; | ||
@Autowired | ||
TopicRepository topicRepo; | ||
|
||
@Test | ||
void shouldSaveTodoToTodoDB() { | ||
Todo todo = new Todo("test"); | ||
Todo saved =todoRepo.save(todo); | ||
Optional<Todo> result= todoRepo.findById(saved.getId()); | ||
assertThat(result).isPresent(); | ||
} | ||
|
||
@Test | ||
void shouldSaveTopicToTopicDB() { | ||
Topic todo = new Topic("test"); | ||
Topic saved =topicRepo.save(todo); | ||
Optional<Topic> result= topicRepo.findById(saved.getId()); | ||
assertThat(result).isPresent(); | ||
} | ||
|
||
} |