From 45de2abf96d1dadcdfea988e2074d6354fc5181c Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 26 Jan 2020 18:04:24 -0300 Subject: [PATCH] [BAEL-3164] Rename module to spring-boot-persistence-2 --- .../spring-boot-persistence-2/HELP.md | 9 ++ .../spring-boot-persistence-2/README.md | 3 + .../spring-boot-persistence-2/pom.xml | 102 +++++++++++++++ .../baeldung/boot/jdbi/JdbiConfiguration.java | 57 +++++++++ .../boot/jdbi/SpringBootJdbiApplication.java | 15 +++ .../baeldung/boot/jdbi/dao/CarMakerDao.java | 35 +++++ .../baeldung/boot/jdbi/dao/CarModelDao.java | 28 ++++ .../baeldung/boot/jdbi/domain/CarMaker.java | 14 ++ .../baeldung/boot/jdbi/domain/CarModel.java | 14 ++ .../boot/jdbi/mapper/CarMakerMapper.java | 27 ++++ .../boot/jdbi/mapper/CarModelMapper.java | 25 ++++ .../boot/jdbi/service/CarMakerService.java | 48 +++++++ .../src/main/resources/application.yml | 1 + .../boot/jdbi/dao/CarMakerDao/findById.sql | 11 ++ .../boot/jdbi/dao/CarMakerDao/insert.sql | 4 + .../dao/CarModelDao/findByMakerIdAndSku.sql | 10 ++ .../boot/jdbi/dao/CarModelDao/insert.sql | 8 ++ .../SpringBootJdbiApplicationUnitTest.java | 121 ++++++++++++++++++ .../src/test/resources/data.sql | 12 ++ .../src/test/resources/schema.sql | 24 ++++ 20 files changed, 568 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-2/HELP.md create mode 100644 persistence-modules/spring-boot-persistence-2/README.md create mode 100644 persistence-modules/spring-boot-persistence-2/pom.xml create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql diff --git a/persistence-modules/spring-boot-persistence-2/HELP.md b/persistence-modules/spring-boot-persistence-2/HELP.md new file mode 100644 index 0000000000..d5a5463718 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/HELP.md @@ -0,0 +1,9 @@ +# Getting Started + +### Reference Documentation +For further reference, please consider the following sections: + +* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) +* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#using-boot-devtools) +* [Spring Configuration Processor](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#configuration-metadata-annotation-processor) + diff --git a/persistence-modules/spring-boot-persistence-2/README.md b/persistence-modules/spring-boot-persistence-2/README.md new file mode 100644 index 0000000000..5d171fb2ca --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) diff --git a/persistence-modules/spring-boot-persistence-2/pom.xml b/persistence-modules/spring-boot-persistence-2/pom.xml new file mode 100644 index 0000000000..51fa56bf17 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + com.baeldung.boot.persistence + spring-boot-persistence-2 + 0.0.1-SNAPSHOT + spring-boot-jdbi + Sample SpringBoot JDBI Project + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../pom.xml + + + + + + org.springframework.boot + spring-boot-dependencies + 2.1.8.RELEASE + pom + import + + + + org.jdbi + jdbi3-spring4 + ${jdbi.version} + + + + org.jdbi + jdbi3-sqlobject + ${jdbi.version} + + + + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.jdbi + jdbi3-spring4 + + + + org.jdbi + jdbi3-sqlobject + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 1.8 + 3.9.1 + + + diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java new file mode 100644 index 0000000000..ddbe6cc118 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java @@ -0,0 +1,57 @@ +package com.baeldung.boot.jdbi; + +import java.util.List; + +import javax.sql.DataSource; + +import org.jdbi.v3.core.Jdbi; +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.spi.JdbiPlugin; +import org.jdbi.v3.sqlobject.SqlObjectPlugin; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; +import org.springframework.transaction.PlatformTransactionManager; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; + +import lombok.extern.slf4j.Slf4j; + +@Configuration +@Slf4j +public class JdbiConfiguration { + @Bean + public Jdbi jdbi(DataSource ds,List jdbiPlugins, List> rowMappers) { + TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds); + Jdbi jdbi = Jdbi.create(proxy); + + // Register all available plugins + log.info("[I27] Installing plugins... ({} found)", jdbiPlugins.size()); + jdbiPlugins.forEach(plugin -> jdbi.installPlugin(plugin)); + + // Register all available rowMappers + log.info("[I31] Installing rowMappers... ({} found)", rowMappers.size()); + rowMappers.forEach(mapper -> jdbi.registerRowMapper(mapper)); + + return jdbi; + } + + @Bean + public JdbiPlugin sqlObjectPlugin() { + return new SqlObjectPlugin(); + } + + @Bean + public CarMakerDao carMakerDao(Jdbi jdbi) { + return jdbi.onDemand(CarMakerDao.class); + } + + @Bean + public CarModelDao carModelDao(Jdbi jdbi) { + return jdbi.onDemand(CarModelDao.class); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java new file mode 100644 index 0000000000..63afe3a3bf --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.boot.jdbi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +public class SpringBootJdbiApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootJdbiApplication.class, args); + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java new file mode 100644 index 0000000000..6cc7268144 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java @@ -0,0 +1,35 @@ +/** + * + */ +package com.baeldung.boot.jdbi.dao; + +import java.util.List; + +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; +import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; +import org.jdbi.v3.sqlobject.statement.SqlBatch; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; + +import com.baeldung.boot.jdbi.domain.CarMaker; + +/** + * @author Philippe + * + */ +@UseClasspathSqlLocator +public interface CarMakerDao { + + @SqlUpdate + @GetGeneratedKeys + Long insert(@BindBean CarMaker carMaker); + + @SqlBatch("insert") + @GetGeneratedKeys + List bulkInsert(@BindBean List carMakers); + + @SqlQuery + CarMaker findById(@Bind("id") Long id); +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java new file mode 100644 index 0000000000..18a05c6108 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java @@ -0,0 +1,28 @@ +package com.baeldung.boot.jdbi.dao; + +import java.util.List; + +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; +import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; +import org.jdbi.v3.sqlobject.statement.SqlBatch; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; + +import com.baeldung.boot.jdbi.domain.CarModel; + +@UseClasspathSqlLocator +public interface CarModelDao { + + @SqlUpdate("insert") + @GetGeneratedKeys + Long insert(@BindBean CarModel carModel); + + @SqlBatch("insert") + @GetGeneratedKeys + List bulkInsert(@BindBean List models); + + @SqlQuery + CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku ); +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java new file mode 100644 index 0000000000..c32b0c30db --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java @@ -0,0 +1,14 @@ +package com.baeldung.boot.jdbi.domain; + +import java.util.List; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class CarMaker { + private Long id; + private String name; + private List models; +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java new file mode 100644 index 0000000000..80b615801b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java @@ -0,0 +1,14 @@ +package com.baeldung.boot.jdbi.domain; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +public class CarModel { + private Long id; + private String name; + private Integer year; + private String sku; + private Long makerId; +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java new file mode 100644 index 0000000000..54fc80d4ab --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java @@ -0,0 +1,27 @@ +package com.baeldung.boot.jdbi.mapper; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; +import org.springframework.stereotype.Component; + +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; + +@Component +public class CarMakerMapper implements RowMapper { + + @Override + public CarMaker map(ResultSet rs, StatementContext ctx) throws SQLException { + CarMaker maker = CarMaker.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .models(new ArrayList()) + .build(); + + return maker; + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java new file mode 100644 index 0000000000..eeceafd649 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java @@ -0,0 +1,25 @@ +package com.baeldung.boot.jdbi.mapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; +import org.springframework.stereotype.Component; + +import com.baeldung.boot.jdbi.domain.CarModel; + +@Component +public class CarModelMapper implements RowMapper{ + + @Override + public CarModel map(ResultSet rs, StatementContext ctx) throws SQLException { + return CarModel.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .sku(rs.getString("sku")) + .year(rs.getInt("year")) + .build(); + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java new file mode 100644 index 0000000000..a058130563 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java @@ -0,0 +1,48 @@ +/** + * + */ +package com.baeldung.boot.jdbi.service; + +import java.util.List; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; + +/** + * @author Philippe + * + */ +@Service +public class CarMakerService { + + private CarMakerDao carMakerDao; + private CarModelDao carModelDao; + + public CarMakerService(CarMakerDao carMakerDao,CarModelDao carModelDao) { + + this.carMakerDao = carMakerDao; + this.carModelDao = carModelDao; + } + + @Transactional + public int bulkInsert(CarMaker carMaker) { + Long carMakerId; + if (carMaker.getId() == null ) { + carMakerId = carMakerDao.insert(carMaker); + carMaker.setId(carMakerId); + } + + // Make sure all models belong to the same maker + carMaker.getModels().forEach(m -> { + m.setMakerId(carMaker.getId()); + carModelDao.insert(m); + }); + + return carMaker.getModels().size(); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml b/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml @@ -0,0 +1 @@ + diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql new file mode 100644 index 0000000000..b36659110a --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql @@ -0,0 +1,11 @@ +-- +-- findById +-- +select + id, + name +from + car_maker +where + id = :id +; diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql new file mode 100644 index 0000000000..0e045d7274 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql @@ -0,0 +1,4 @@ +-- +-- Insert +-- +insert into car_maker(id,name) values (:id,:name); diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql new file mode 100644 index 0000000000..270d9baaa8 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql @@ -0,0 +1,10 @@ +-- +-- Insert +-- +select * +from + car_model +where + maker_fk = :makerId and + sku = :sku +; \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql new file mode 100644 index 0000000000..b277213584 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql @@ -0,0 +1,8 @@ +-- +-- Insert +-- +insert into car_model(maker_fk,name,sku,year) values ( + :makerId, + :name, + :sku, + :year ); diff --git a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java new file mode 100644 index 0000000000..e4b623ee2b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java @@ -0,0 +1,121 @@ +package com.baeldung.boot.jdbi; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.jdbi.v3.core.Jdbi; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; +import com.baeldung.boot.jdbi.service.CarMakerService; + +import lombok.extern.slf4j.Slf4j; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class SpringBootJdbiApplicationUnitTest { + + + @Autowired + private CarMakerDao carMakerDao; + + @Autowired + private CarModelDao carModelDao; + + @Autowired + private CarMakerService carMakerService; + + @Test + public void givenNewCarMaker_whenInsertNewCarMaker_thenSuccess() { + + assertNotNull(carMakerDao); + + CarMaker carMaker = CarMaker.builder() + .name("Diamond Motors") + .build(); + + Long generatedId = carMakerDao.insert(carMaker); + log.info("[I37] generatedId = {}", generatedId); + assertThat(generatedId).isGreaterThan(0); + } + + @Test + public void givenNewCarMakers_whenInsertNewCarMakers_thenSuccess() { + + assertNotNull(carMakerDao); + + CarMaker carMaker1 = CarMaker.builder() + .name("maker1") + .build(); + + CarMaker carMaker2 = CarMaker.builder() + .name("maker2") + .build(); + + List makers = new ArrayList<>(); + makers.add(carMaker1); + makers.add(carMaker2); + + List generatedIds = carMakerDao.bulkInsert(makers); + log.info("[I37] generatedIds = {}", generatedIds); + assertThat(generatedIds).size().isEqualTo(makers.size()); + } + + + @Test + public void givenExistingCarMaker_whenFindById_thenReturnExistingCarMaker() { + + CarMaker maker = carMakerDao.findById(1l); + assertThat(maker).isNotNull(); + assertThat(maker.getId()).isEqualTo(1); + + } + + @Test + public void givenExistingCarMaker_whenBulkInsertFails_thenRollback() { + + CarMaker maker = carMakerDao.findById(1l); + CarModel m1 = CarModel.builder() + .makerId(maker.getId()) + .name("Model X1") + .sku("1-M1") + .year(2019) + .build(); + maker.getModels().add(m1); + + CarModel m2 = CarModel.builder() + .makerId(maker.getId()) + .name("Model X1") + .sku("1-M1") + .year(2019) + .build(); + maker.getModels().add(m2); + + // This insert fails because we have the same SKU + try { + carMakerService.bulkInsert(maker); + assertTrue("Insert must fail", true); + } + catch(Exception ex) { + log.info("[I113] Exception: {}", ex.getMessage()); + } + + CarModel m = carModelDao.findByMakerIdAndSku(maker.getId(), "1-M1"); + assertThat(m).isNull(); + + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql b/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql new file mode 100644 index 0000000000..e3e1f4ae32 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql @@ -0,0 +1,12 @@ + +insert into car_maker(id,name) values (1,'Special Motors'); +insert into car_maker(id,name) values (2,'BWM'); +insert into car_maker(id,name) values (3,'Dolores'); + +insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018); +insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008); + +insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008); +insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009); +insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008); + diff --git a/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql new file mode 100644 index 0000000000..a0d0eaf62e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql @@ -0,0 +1,24 @@ +-- +-- Car makers table +-- +create table car_maker( + id identity, + name varchar(128) not null +); + +create unique index ui_car_maker_01 on car_maker(name); + +-- +-- Car models table +-- +create table car_model( + id identity, + maker_fk int not null, + name varchar(128) not null, + sku varchar(128) not null, + year int not null +); + +create unique index ui_car_model_01 on car_model(maker_fk,sku); +create unique index ui_car_model_02 on car_model(maker_fk,name,year); +