├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── sctrcd │ └── multidsdemo │ ├── domain │ ├── bar │ │ └── Bar.java │ └── foo │ │ └── Foo.java │ └── integration │ ├── config │ ├── AppConfig.java │ ├── bar │ │ └── BarConfig.java │ └── foo │ │ └── FooConfig.java │ └── repositories │ ├── bar │ └── BarRepository.java │ └── foo │ └── FooRepository.java └── test ├── java └── com │ └── sctrcd │ └── multidsdemo │ └── integration │ └── repositories │ ├── bar │ └── BarRepositoryTest.java │ └── foo │ └── FooRepositoryTest.java └── resources └── log4j.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.settings 3 | /.classpath 4 | /.project 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring-data multiple datasource demo 2 | ==================================== 3 | 4 | This project generates a .jar with a couple of Spring Data repositories and associated tests. 5 | I have had trouble getting this structure to work, so this project is intended to simplify 6 | things as much as possible to establish how best to do it. 7 | 8 | It's built with Maven, so from the command line you can run a full build (including tests): 9 | 10 | mvn clean install 11 | 12 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | com.sctrcd 8 | multids-demo 9 | 1.0.0-SNAPSHOT 10 | Spring-data multiple datasource demo 11 | jar 12 | 13 | A application demonstrating how to use Spring Data repositories with multiple datasources. 14 | 15 | 16 | 17 | 1.7 18 | UTF-8 19 | 3.2.5.RELEASE 20 | 21 | 22 | 23 | 24 | Version99 25 | Version 99 Does Not Exist Maven repository 26 | 27 | default 28 | http://version99.qos.ch/ 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.slf4j 36 | slf4j-api 37 | 1.6.4 38 | 39 | 40 | commons-logging 41 | commons-logging 42 | 99-empty 43 | 44 | 45 | org.slf4j 46 | slf4j-log4j12 47 | 1.6.4 48 | 49 | 50 | org.slf4j 51 | jcl-over-slf4j 52 | 1.6.4 53 | 54 | 55 | log4j 56 | log4j 57 | 1.2.16 58 | 59 | 60 | 61 | 62 | 63 | 64 | 66 | 67 | org.springframework 68 | spring-context-support 69 | ${org.springframework.version} 70 | 71 | 72 | 73 | org.springframework.data 74 | spring-data-jpa 75 | 1.4.2.RELEASE 76 | 77 | 78 | 80 | 81 | org.springframework 82 | spring-orm 83 | ${org.springframework.version} 84 | 85 | 86 | 88 | 89 | org.springframework 90 | spring-oxm 91 | ${org.springframework.version} 92 | 93 | 94 | 97 | 98 | org.springframework 99 | spring-webmvc 100 | ${org.springframework.version} 101 | 102 | 103 | org.springframework 104 | spring-web 105 | ${org.springframework.version} 106 | 107 | 108 | 111 | 112 | org.springframework 113 | spring-test 114 | ${org.springframework.version} 115 | test 116 | 117 | 118 | 119 | org.hibernate 120 | hibernate-core 121 | 4.0.1.Final 122 | 123 | 124 | org.hibernate 125 | hibernate-entitymanager 126 | 4.0.1.Final 127 | 128 | 129 | org.hibernate 130 | hibernate-commons-annotations 131 | 3.2.0.Final 132 | 133 | 134 | org.hibernate.javax.persistence 135 | hibernate-jpa-2.0-api 136 | 1.0.1.Final 137 | 138 | 139 | 140 | 141 | javax.validation 142 | validation-api 143 | 1.0.0.GA 144 | 145 | 146 | org.hibernate 147 | hibernate-validator 148 | 4.2.0.Final 149 | 150 | 151 | org.hibernate 152 | hibernate-ehcache 153 | 4.0.0.Final 154 | 155 | 156 | 157 | org.hsqldb 158 | hsqldb 159 | 2.2.9 160 | 161 | 162 | 163 | cglib 164 | cglib-nodep 165 | 2.2.2 166 | 167 | 168 | 169 | commons-dbcp 170 | commons-dbcp 171 | 1.4 172 | 173 | 174 | commons-beanutils 175 | commons-beanutils 176 | 1.8.3 177 | 178 | 179 | 180 | org.slf4j 181 | slf4j-api 182 | 1.6.4 183 | 184 | 185 | commons-logging 186 | commons-logging 187 | 99-empty 188 | 189 | 190 | org.slf4j 191 | slf4j-log4j12 192 | 1.6.4 193 | 194 | 195 | org.slf4j 196 | jcl-over-slf4j 197 | 1.6.4 198 | 199 | 200 | log4j 201 | log4j 202 | 1.2.16 203 | 204 | 205 | 206 | junit 207 | junit 208 | 4.8.1 209 | 212 | provided 213 | 214 | 215 | org.hamcrest 216 | hamcrest-all 217 | 1.1 218 | test 219 | 220 | 221 | org.mockito 222 | mockito-all 223 | 1.8.4 224 | test 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | src/main/resources 233 | 234 | 235 | 236 | 237 | maven-compiler-plugin 238 | 2.3.2 239 | 240 | ${java.version} 241 | ${java.version} 242 | 243 | 244 | 245 | 246 | 247 | 248 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/domain/bar/Bar.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.domain.bar; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Bar { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.AUTO) 14 | @Column 15 | private Long id; 16 | 17 | @Column 18 | private String name; 19 | 20 | public Bar() { 21 | } 22 | 23 | public Bar(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Long id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/domain/foo/Foo.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.domain.foo; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Foo { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.AUTO) 14 | @Column 15 | private Long id; 16 | 17 | @Column 18 | private String name; 19 | 20 | public Foo() { 21 | } 22 | 23 | public Foo(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Long id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/integration/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.orm.hibernate4.HibernateExceptionTranslator; 7 | import org.springframework.orm.jpa.JpaVendorAdapter; 8 | import org.springframework.orm.jpa.vendor.Database; 9 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 10 | 11 | @Configuration 12 | @ComponentScan(basePackages = { "com.sctrcd.multidsdemo" }) 13 | public class AppConfig { 14 | 15 | /** 16 | * Primary because if we have activated embedded databases, we do not want 17 | * the application to connect to an external database. 18 | */ 19 | @Bean 20 | public JpaVendorAdapter jpaVendorAdapter() { 21 | HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 22 | jpaVendorAdapter.setShowSql(true); 23 | jpaVendorAdapter.setGenerateDdl(true); 24 | jpaVendorAdapter.setDatabase(Database.HSQL); 25 | return jpaVendorAdapter; 26 | } 27 | 28 | @Bean 29 | public HibernateExceptionTranslator hibernateExceptionTranslator() { 30 | return new HibernateExceptionTranslator(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/integration/config/bar/BarConfig.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.config.bar; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.EntityManagerFactory; 5 | import javax.sql.DataSource; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 11 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 12 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 13 | import org.springframework.orm.jpa.JpaTransactionManager; 14 | import org.springframework.orm.jpa.JpaVendorAdapter; 15 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 16 | import org.springframework.transaction.PlatformTransactionManager; 17 | import org.springframework.transaction.annotation.EnableTransactionManagement; 18 | 19 | @Configuration 20 | @EnableTransactionManagement 21 | @EnableJpaRepositories( 22 | entityManagerFactoryRef = "barEntityManagerFactory", 23 | transactionManagerRef = "barTransactionManager", 24 | basePackages = { "com.sctrcd.multidsdemo.integration.repositories.bar" }) 25 | public class BarConfig { 26 | 27 | @Autowired 28 | JpaVendorAdapter jpaVendorAdapter; 29 | 30 | /** 31 | * Primary because if we have activated embedded databases, we do not want 32 | * the application to connect to an external database. 33 | */ 34 | @Bean(name = "barDataSource") 35 | public DataSource dataSource() { 36 | return new EmbeddedDatabaseBuilder() 37 | .setName("bardb") 38 | .setType(EmbeddedDatabaseType.HSQL) 39 | .build(); 40 | } 41 | 42 | @Bean(name = "barEntityManager") 43 | public EntityManager entityManager() { 44 | return entityManagerFactory().createEntityManager(); 45 | } 46 | 47 | @Bean(name = "barEntityManagerFactory") 48 | public EntityManagerFactory entityManagerFactory() { 49 | LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 50 | lef.setDataSource(dataSource()); 51 | lef.setJpaVendorAdapter(jpaVendorAdapter); 52 | lef.setPackagesToScan("com.sctrcd.multidsdemo.domain.bar"); 53 | lef.setPersistenceUnitName("barPersistenceUnit"); 54 | lef.afterPropertiesSet(); 55 | return lef.getObject(); 56 | } 57 | 58 | @Bean(name = "barTransactionManager") 59 | public PlatformTransactionManager transactionManager() { 60 | return new JpaTransactionManager(entityManagerFactory()); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/integration/config/foo/FooConfig.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.config.foo; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.EntityManagerFactory; 5 | import javax.sql.DataSource; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 11 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 12 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 13 | import org.springframework.orm.jpa.JpaTransactionManager; 14 | import org.springframework.orm.jpa.JpaVendorAdapter; 15 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 16 | import org.springframework.transaction.PlatformTransactionManager; 17 | import org.springframework.transaction.annotation.EnableTransactionManagement; 18 | 19 | @Configuration 20 | @EnableTransactionManagement 21 | @EnableJpaRepositories( 22 | entityManagerFactoryRef = "fooEntityManagerFactory", 23 | transactionManagerRef = "fooTransactionManager", 24 | basePackages = {"com.sctrcd.multidsdemo.integration.repositories.foo"}) 25 | public class FooConfig { 26 | 27 | @Autowired 28 | JpaVendorAdapter jpaVendorAdapter; 29 | 30 | /** 31 | * Primary because if we have activated embedded databases, we do not want 32 | * the application to connect to an external database. 33 | */ 34 | @Bean(name = "fooDataSource") 35 | public DataSource dataSource() { 36 | return new EmbeddedDatabaseBuilder() 37 | .setName("foodb") 38 | .setType(EmbeddedDatabaseType.HSQL) 39 | .build(); 40 | } 41 | 42 | @Bean(name = "fooEntityManager") 43 | public EntityManager entityManager() { 44 | return entityManagerFactory().createEntityManager(); 45 | } 46 | 47 | @Bean(name = "fooEntityManagerFactory") 48 | public EntityManagerFactory entityManagerFactory() { 49 | LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 50 | lef.setDataSource(dataSource()); 51 | lef.setJpaVendorAdapter(jpaVendorAdapter); 52 | lef.setPackagesToScan("com.sctrcd.multidsdemo.domain.foo"); 53 | lef.setPersistenceUnitName("fooPersistenceUnit"); 54 | lef.afterPropertiesSet(); 55 | return lef.getObject(); 56 | } 57 | 58 | @Bean(name = "fooTransactionManager") 59 | public PlatformTransactionManager transactionManager() { 60 | return new JpaTransactionManager(entityManagerFactory()); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/integration/repositories/bar/BarRepository.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.repositories.bar; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.sctrcd.multidsdemo.domain.bar.Bar; 6 | 7 | public interface BarRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sctrcd/multidsdemo/integration/repositories/foo/FooRepository.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.repositories.foo; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.sctrcd.multidsdemo.domain.foo.Foo; 6 | 7 | public interface FooRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/test/java/com/sctrcd/multidsdemo/integration/repositories/bar/BarRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.repositories.bar; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.support.AnnotationConfigContextLoader; 13 | 14 | import com.sctrcd.multidsdemo.domain.bar.Bar; 15 | import com.sctrcd.multidsdemo.integration.config.AppConfig; 16 | import com.sctrcd.multidsdemo.integration.repositories.bar.BarRepository; 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) 20 | public class BarRepositoryTest { 21 | 22 | private static Logger log = LoggerFactory.getLogger(BarRepositoryTest.class); 23 | 24 | @Autowired 25 | BarRepository barRepo; 26 | 27 | @Test 28 | public void test() { 29 | try { 30 | Bar bar = new Bar("bar"); 31 | barRepo.saveAndFlush(bar); 32 | } catch (Exception e) { 33 | log.error("Error saving Bar.", e); 34 | throw e; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/sctrcd/multidsdemo/integration/repositories/foo/FooRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package com.sctrcd.multidsdemo.integration.repositories.foo; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.support.AnnotationConfigContextLoader; 13 | 14 | import com.sctrcd.multidsdemo.domain.foo.Foo; 15 | import com.sctrcd.multidsdemo.integration.config.AppConfig; 16 | import com.sctrcd.multidsdemo.integration.repositories.foo.FooRepository; 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) 20 | public class FooRepositoryTest { 21 | 22 | private static Logger log = LoggerFactory.getLogger(FooRepositoryTest.class); 23 | 24 | @Autowired 25 | FooRepository fooRepo; 26 | 27 | @Test 28 | public void test() { 29 | try { 30 | Foo foo = new Foo("foo"); 31 | fooRepo.saveAndFlush(foo); 32 | } catch (Exception e) { 33 | log.error("Error saving Foo.", e); 34 | throw e; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | --------------------------------------------------------------------------------