From db66b173b8ab864fe7dc4904d39f19859dbe38d9 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 23 Jan 2019 10:07:54 +0100 Subject: [PATCH] #441 - Demonstrating how to do direct inserts with the JdbcAggregateTemplate. See also: https://jira.spring.io/browse/DATAJDBC-282 --- jdbc/basics/pom.xml | 8 +++++ .../aggregate/AggregateConfiguration.java | 7 ++++ .../simpleentity/CategoryConfiguration.java | 18 ++++++++++ .../simpleentity/CategoryRepository.java | 2 +- .../jdbc/basics/simpleentity/WithInsert.java | 23 +++++++++++++ .../basics/simpleentity/WithInsertImpl.java | 34 +++++++++++++++++++ .../simpleentity/SimpleEntityTests.java | 11 +++++- 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsert.java create mode 100644 jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsertImpl.java diff --git a/jdbc/basics/pom.xml b/jdbc/basics/pom.xml index 47056a01..a85b176a 100644 --- a/jdbc/basics/pom.xml +++ b/jdbc/basics/pom.xml @@ -21,4 +21,12 @@ + + + org.springframework.data + spring-data-jdbc + 1.1.0.BUILD-SNAPSHOT + + + \ No newline at end of file diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AggregateConfiguration.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AggregateConfiguration.java index 10e4c384..4c9b45dc 100644 --- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AggregateConfiguration.java +++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AggregateConfiguration.java @@ -29,6 +29,8 @@ import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; import org.springframework.data.jdbc.repository.config.JdbcConfiguration; import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.lang.Nullable; /** @@ -86,4 +88,9 @@ public class AggregateConfiguration extends JdbcConfiguration { } })); } + + @Bean + public NamedParameterJdbcTemplate namedParameterJdbcTemplate(JdbcOperations operations) { + return new NamedParameterJdbcTemplate(operations); + } } diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryConfiguration.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryConfiguration.java index 38cb3857..74bca453 100644 --- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryConfiguration.java +++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryConfiguration.java @@ -16,14 +16,25 @@ package example.springdata.jdbc.basics.simpleentity; import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.data.jdbc.core.DataAccessStrategy; +import org.springframework.data.jdbc.core.DefaultDataAccessStrategy; +import org.springframework.data.jdbc.core.JdbcAggregateOperations; +import org.springframework.data.jdbc.core.JdbcAggregateTemplate; +import org.springframework.data.jdbc.core.SqlGeneratorSource; import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; import org.springframework.data.jdbc.repository.config.JdbcConfiguration; +import org.springframework.data.relational.core.conversion.RelationalConverter; +import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; import org.springframework.data.relational.core.mapping.event.RelationalEvent; +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; /** * Contains infrastructure necessary for creating repositories and two listeners. @@ -61,4 +72,11 @@ public class CategoryConfiguration { } }; } + + // the following bean definitions are only necessary for providing JdbcAggregateOperations for injection into WithInsertImpl. + @Bean + public NamedParameterJdbcOperations namedParameterJdbcOperations(JdbcOperations operations) { + return new NamedParameterJdbcTemplate(operations); + } + } diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryRepository.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryRepository.java index 5fb52fd7..70927286 100644 --- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryRepository.java +++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryRepository.java @@ -22,4 +22,4 @@ import org.springframework.data.repository.CrudRepository; * * @author Jens Schauder */ -interface CategoryRepository extends CrudRepository {} +interface CategoryRepository extends CrudRepository, WithInsert {} diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsert.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsert.java new file mode 100644 index 00000000..dbe42152 --- /dev/null +++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsert.java @@ -0,0 +1,23 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jdbc.basics.simpleentity; + +/** + * @author Jens Schauder + */ +public interface WithInsert { + T insert(T t); +} diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsertImpl.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsertImpl.java new file mode 100644 index 00000000..124ca720 --- /dev/null +++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/WithInsertImpl.java @@ -0,0 +1,34 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jdbc.basics.simpleentity; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jdbc.core.JdbcAggregateTemplate; +import org.springframework.data.relational.core.mapping.event.WithId; + +/** + * @author Jens Schauder + */ +public class WithInsertImpl implements WithInsert { + @Autowired + JdbcAggregateTemplate template; + + + @Override + public T insert(T t) { + return template.insert(t); + } +} diff --git a/jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java index fcd4a121..bf1ecfed 100644 --- a/jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java +++ b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java @@ -43,7 +43,7 @@ public class SimpleEntityTests { public void exerciseRepositoryForSimpleEntity() { // create some categories - Category cars = repository.save(new Category("Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8)); + Category cars = repository.save(new Category("Cars", "Anything that has approximately 4 wheels.", AgeGroup._3to8)); Category buildings = repository.save(new Category("Buildings", null, AgeGroup._12andOlder)); // save categories @@ -61,4 +61,13 @@ public class SimpleEntityTests { repository.delete(cars); Output.list(repository.findAll(), "`Cars` is gone."); } + + @Test + public void directInsert(){ + + Category cars = new Category("Cars", "Anything that has approximately 4 wheels.", AgeGroup._3to8).withId(23L); + repository.insert(cars); + + Output.list(repository.findAll(), "`Cars` inserted with id 23L"); + } }