#441 - Demonstrating how to do direct inserts with the JdbcAggregateTemplate.
See also: https://jira.spring.io/browse/DATAJDBC-282
This commit is contained in:
committed by
Mark Paluch
parent
f27645dbb6
commit
db66b173b8
@@ -21,4 +21,12 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jdbc</artifactId>
|
||||
<version>1.1.0.BUILD-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,4 +22,4 @@ import org.springframework.data.repository.CrudRepository;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
interface CategoryRepository extends CrudRepository<Category, Long> {}
|
||||
interface CategoryRepository extends CrudRepository<Category, Long>, WithInsert<Category> {}
|
||||
|
||||
@@ -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> {
|
||||
T insert(T t);
|
||||
}
|
||||
@@ -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<T> implements WithInsert<T> {
|
||||
@Autowired
|
||||
JdbcAggregateTemplate template;
|
||||
|
||||
|
||||
@Override
|
||||
public T insert(T t) {
|
||||
return template.insert(t);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user