#313 - Polishing.

Tweaking indentation in pom.xml to use tabs. Switch to upgrade to Spring Data release train Lovelace to simplify dependency declarations.

Import order. Copyright years. Formatting.

Original pull request: #324.
This commit is contained in:
Oliver Gierke
2018-02-22 13:33:13 +01:00
parent 5d556d6d45
commit 74d947e951
14 changed files with 114 additions and 132 deletions

View File

@@ -13,4 +13,5 @@
<name>Spring Data JDBC - Basic usage examples</name>
<description>Sample project demonstrating Spring Data JDBC features</description>
</project>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -15,6 +15,14 @@
*/
package example.springdata.jdbc.basics.aggregate;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sql.DataSource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -24,21 +32,16 @@ import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.event.BeforeSave;
import org.springframework.data.jdbc.mapping.model.*;
import org.springframework.data.jdbc.mapping.model.ConversionCustomizer;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.lang.Nullable;
import javax.sql.DataSource;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Jens Schauder
*/
@@ -54,14 +57,17 @@ public class AggregateConfiguration {
return (ApplicationListener<BeforeSave>) event -> {
Object entity = event.getEntity();
if (entity instanceof LegoSet) {
LegoSet legoSet = (LegoSet) entity;
if (legoSet.getId() == 0) {
legoSet.setId(id.incrementAndGet());
}
Manual manual = legoSet.getManual();
if (manual != null) {
manual.setId((long) legoSet.getId());
}
@@ -98,8 +104,7 @@ public class AggregateConfiguration {
@Override
public String getTableName(Class<?> type) {
return tableAliases.computeIfAbsent(super.getTableName(type),key -> key);
return tableAliases.computeIfAbsent(super.getTableName(type), key -> key);
}
@Override
@@ -128,6 +133,7 @@ public class AggregateConfiguration {
return Math.toIntExact(clob.length()) == 0 //
? "" //
: clob.getSubString(1, Math.toIntExact(clob.length()));
} catch (SQLException e) {
throw new IllegalStateException("Failed to convert CLOB to String.", e);
}
@@ -140,7 +146,6 @@ public class AggregateConfiguration {
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {
NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);
DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();
accessStrategy.setDelegate(new DefaultDataAccessStrategy( //

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -16,16 +16,17 @@
package example.springdata.jdbc.basics.aggregate;
import lombok.Data;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
/**
* A Lego Set consisting of multiple Blocks and a manual
*
@@ -35,25 +36,21 @@ import java.util.Map;
@AccessType(Type.PROPERTY)
public class LegoSet {
@Id
private int id;
private @Id int id;
private String name;
@Transient
private Period minimumAge;
@Transient
private Period maximumAge;
private @Transient Period minimumAge, maximumAge;
/**
* Since Manuals are part of a {@link LegoSet} and only make sense inside a {@link LegoSet} it is considered part of the Aggregate.
* Since Manuals are part of a {@link LegoSet} and only make sense inside a {@link LegoSet} it is considered part of
* the Aggregate.
*/
private Manual manual;
// You can build multiple models from one LegoSet
private final Map<String, Model> models = new HashMap<>();
// conversion for custom types currently has to be done through getters/setter + marking the underlying property with @Transient.
// conversion for custom types currently has to be done through getters/setter + marking the underlying property with
// @Transient.
public int getIntMinimumAge() {
return toInt(this.minimumAge);
}
@@ -70,7 +67,6 @@ public class LegoSet {
maximumAge = toPeriod(years);
}
private static int toInt(Period period) {
return (int) (period == null ? 0 : period.get(ChronoUnit.YEARS));
}
@@ -84,6 +80,7 @@ public class LegoSet {
Model model = new Model();
model.name = name;
model.description = description;
models.put(name, model);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -19,8 +19,7 @@ import org.springframework.data.repository.CrudRepository;
/**
* A repository for {@link LegoSet}.
*
* @author Jens Schauder
*/
public interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {
}
interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -15,10 +15,10 @@
*/
package example.springdata.jdbc.basics.aggregate;
import org.springframework.data.annotation.Id;
import lombok.Data;
import org.springframework.data.annotation.Id;
/**
* A manual instructing how to assemble a {@link LegoSet}.
*
@@ -27,6 +27,8 @@ import lombok.Data;
@Data
public class Manual {
private @Id Long id;
private String author, text;
Manual(String text, String author) {
@@ -34,10 +36,4 @@ public class Manual {
this.author = author;
this.text = text;
}
@Id
private Long id;
private String author;
private String text;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -16,20 +16,17 @@
package example.springdata.jdbc.basics.aggregate;
import lombok.ToString;
import org.springframework.data.annotation.Id;
/**
* One of potentially multiple models that can be build from a single {@link LegoSet}.
*
* No getters or setters needed.
* One of potentially multiple models that can be build from a single {@link LegoSet}. No getters or setters needed.
*
* @author Jens Schauder
*/
@ToString
public class Model {
@Id
String name;
@Id String name;
String description;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -15,15 +15,15 @@
*/
package example.springdata.jdbc.basics.simpleentity;
import java.time.LocalDateTime;
import example.springdata.jdbc.basics.aggregate.AgeGroup;
import example.springdata.jdbc.basics.aggregate.LegoSet;
import org.springframework.data.annotation.Id;
import lombok.Data;
import lombok.Setter;
import java.time.LocalDateTime;
import org.springframework.data.annotation.Id;
/**
* Coarse classification for {@link LegoSet}s, like "Car", "Plane", "Building" and so on.
*
@@ -32,6 +32,12 @@ import lombok.Setter;
@Data
public class Category {
private final @Id Long id;
private String name, description;
private LocalDateTime created = LocalDateTime.now();
private @Setter long inserted;
private AgeGroup ageGroup;
public Category(String name, String description, AgeGroup ageGroup) {
this.id = null;
@@ -40,23 +46,10 @@ public class Category {
this.ageGroup = ageGroup;
}
@Id
private final Long id;
private String name;
private String description;
private LocalDateTime created = LocalDateTime.now();
@Setter
private long inserted;
public void timeStamp() {
if (inserted == 0) {
inserted = System.currentTimeMillis();
}
}
private AgeGroup ageGroup;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -15,6 +15,8 @@
*/
package example.springdata.jdbc.basics.simpleentity;
import javax.sql.DataSource;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
@@ -30,8 +32,6 @@ import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.sql.DataSource;
/**
* Contains infrastructure necessary for creating repositories and two listeners.
* <p>
@@ -43,24 +43,23 @@ import javax.sql.DataSource;
@EnableJdbcRepositories
public class CategoryConfiguration {
@Bean
public ApplicationListener<?> loggingListener() {
return (ApplicationListener<ApplicationEvent>) event -> {
if (event instanceof JdbcEvent) {
System.out.println("received an event: " + event);
System.out.println("Received an event: " + event);
}
};
}
@Bean
public ApplicationListener<BeforeSave> timeStampingSaveTime() {
return event -> {
Object entity = event.getEntity();
if (entity instanceof Category) {
Category category = (Category) entity;
category.timeStamp();
@@ -73,7 +72,6 @@ public class CategoryConfiguration {
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {
NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);
DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();
accessStrategy.setDelegate(new DefaultDataAccessStrategy( //

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -22,6 +22,4 @@ import org.springframework.data.repository.CrudRepository;
*
* @author Jens Schauder
*/
public interface CategoryRepository extends CrudRepository<Category, Long> {
}
interface CategoryRepository extends CrudRepository<Category, Long> {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -15,11 +15,11 @@
*/
package example.springdata.jdbc.basics;
import lombok.experimental.UtilityClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.experimental.UtilityClass;
/**
* Trivial class to print domain objects to the console in a somewhat readable format.
*
@@ -34,9 +34,7 @@ public class Output {
StringBuilder message = new StringBuilder(String.format("==== %s ====\n", title));
categories.forEach(category -> {
message.append(category.toString().replace(", ", ",\n\t"));
});
categories.forEach(category -> message.append(category.toString().replace(", ", ",\n\t")));
LOG.info(message.toString());
}

View File

@@ -1,5 +1,5 @@
package example.springdata.jdbc.basics.aggregate;/*
* Copyright 2017 the original author or authors.
/*
* Copyright 2017-2018 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.
@@ -13,8 +13,12 @@ package example.springdata.jdbc.basics.aggregate;/*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.basics.aggregate;
import example.springdata.jdbc.basics.Output;
import java.time.Period;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,8 +26,6 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.Period;
/**
* Demonstrates various possibilities to customize the behavior of a repository.
*
@@ -34,8 +36,7 @@ import java.time.Period;
@AutoConfigureJdbc
public class AggregateTests {
@Autowired
private LegoSetRepository repository;
@Autowired LegoSetRepository repository;
@Test
public void exerciseSomewhatComplexEntity() {
@@ -63,9 +64,11 @@ public class AggregateTests {
private LegoSet createLegoSet() {
LegoSet smallCar = new LegoSet();
smallCar.setName("Small Car 01");
smallCar.setMinimumAge(Period.ofYears(5));
smallCar.setMaximumAge(Period.ofYears(12));
return smallCar;
}
}

View File

@@ -1,5 +1,5 @@
package example.springdata.jdbc.basics.simpleentity;/*
* Copyright 2017 the original author or authors.
/*
* Copyright 2017-2018 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.
@@ -13,9 +13,14 @@ package example.springdata.jdbc.basics.simpleentity;/*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.basics.simpleentity;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jdbc.basics.Output;
import example.springdata.jdbc.basics.aggregate.AgeGroup;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,9 +28,6 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Demonstrates simple CRUD operations with a simple entity without any references.
*
@@ -36,8 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@AutoConfigureJdbc
public class SimpleEntityTests {
@Autowired
private CategoryRepository repository;
@Autowired CategoryRepository repository;
@Test
public void exerciseRepositoryForSimpleEntity() {
@@ -62,6 +63,5 @@ public class SimpleEntityTests {
// delete stuff again
repository.delete(cars);
Output.list(repository.findAll(), "`Cars` is gone.");
}
}

View File

@@ -1,43 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jdbc-examples</artifactId>
<packaging>pom</packaging>
<artifactId>spring-data-jdbc-examples</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Data JDBC - Examples</name>
<description>Sample projects for Spring Data JDBC</description>
<url>http://projects.spring.io/spring-data-jdbc</url>
<inceptionYear>2017</inceptionYear>
<name>Spring Data JDBC - Examples</name>
<description>Sample projects for Spring Data JDBC</description>
<url>http://projects.spring.io/spring-data-jdbc</url>
<inceptionYear>2017</inceptionYear>
<modules>
<module>basics</module>
</modules>
<modules>
<module>basics</module>
</modules>
<dependencies>
<properties>
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
</project>