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.
Merge pull request eugenp#127 from Doha2012/master
fix jpa multiple db
- Loading branch information
Showing
9 changed files
with
81 additions
and
93 deletions.
There are no files selected for viewing
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
54 changes: 0 additions & 54 deletions
54
spring-jpa/src/main/java/org/baeldung/config/MultipleDBJPAConfig.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
spring-jpa/src/main/java/org/baeldung/config/MyJtaPlatform.java
This file was deleted.
Oops, something went wrong.
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
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
11 changes: 11 additions & 0 deletions
11
spring-jpa/src/main/resources/persistence-multiple-db.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,11 @@ | ||
# jdbc.X | ||
jdbc.driverClassName=com.mysql.jdbc.Driver | ||
user.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_user?createDatabaseIfNotExist=true | ||
product.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_product?createDatabaseIfNotExist=true | ||
jdbc.user=tutorialuser | ||
jdbc.pass=tutorialmy5ql | ||
|
||
# hibernate.X | ||
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect | ||
hibernate.show_sql=false | ||
hibernate.hbm2ddl.auto=create-drop |
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,8 @@ | ||
package org.baeldung.persistence.service; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import org.baeldung.config.MultipleDBJPAConfig; | ||
import org.baeldung.config.ProductConfig; | ||
import org.baeldung.config.UserConfig; | ||
import org.baeldung.persistence.multiple.dao.product.ProductRepository; | ||
|
@@ -12,15 +12,15 @@ | |
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.transaction.TransactionConfiguration; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = { MultipleDBJPAConfig.class, UserConfig.class, ProductConfig.class }) | ||
@Transactional | ||
@TransactionConfiguration(transactionManager = "transactionManager") | ||
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class }) | ||
@TransactionConfiguration | ||
public class JPAMultipleDBTest { | ||
@Autowired | ||
private UserRepository userRepository; | ||
|
@@ -29,16 +29,41 @@ public class JPAMultipleDBTest { | |
private ProductRepository productRepository; | ||
|
||
@Test | ||
@Transactional("userTransactionManager") | ||
public void whenCreatingUser_thenCreated() { | ||
User user = new User(); | ||
user.setName("John"); | ||
user.setEmail("[email protected]"); | ||
user.setAge(20); | ||
user = userRepository.save(user); | ||
|
||
assertNotNull(userRepository.findOne(user.getId())); | ||
} | ||
|
||
@Test | ||
@Transactional("userTransactionManager") | ||
public void whenCreatingUsersWithSameEmail_thenRollback() { | ||
User user1 = new User(); | ||
user1.setName("John"); | ||
user1.setEmail("[email protected]"); | ||
user1.setAge(20); | ||
user1 = userRepository.save(user1); | ||
assertNotNull(userRepository.findOne(user1.getId())); | ||
|
||
User user2 = new User(); | ||
user2.setName("Tom"); | ||
user2.setEmail("[email protected]"); | ||
user2.setAge(10); | ||
try { | ||
user2 = userRepository.save(user2); | ||
} catch (final DataIntegrityViolationException e) { | ||
} | ||
|
||
assertNull(userRepository.findOne(user2.getId())); | ||
} | ||
|
||
@Test | ||
@Transactional("productTransactionManager") | ||
public void whenCreatingProduct_thenCreated() { | ||
Product product = new Product(); | ||
product.setName("Book"); | ||
|
@@ -48,4 +73,5 @@ public void whenCreatingProduct_thenCreated() { | |
|
||
assertNotNull(productRepository.findOne(product.getId())); | ||
} | ||
|
||
} |