diff --git a/jdbc/basics/pom.xml b/jdbc/basics/pom.xml
index 823367a0..68779d8f 100644
--- a/jdbc/basics/pom.xml
+++ b/jdbc/basics/pom.xml
@@ -13,4 +13,5 @@
Spring Data JDBC - Basic usage examplesSample project demonstrating Spring Data JDBC features
+
\ No newline at end of file
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AgeGroup.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AgeGroup.java
index 68b3cd96..dde6a343 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AgeGroup.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/AgeGroup.java
@@ -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.
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 c4f6f997..73a59e71 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
@@ -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) 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( //
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSet.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSet.java
index 3c2e2e09..e2b88e84 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSet.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSet.java
@@ -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 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);
}
}
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSetRepository.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSetRepository.java
index 2718fda9..5c33c5d1 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSetRepository.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSetRepository.java
@@ -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 {
-
-}
+interface LegoSetRepository extends CrudRepository {}
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Manual.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Manual.java
index 5e5185b2..b99a3251 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Manual.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Manual.java
@@ -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;
}
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Model.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Model.java
index 7130a032..6eb1bbf8 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Model.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Model.java
@@ -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;
}
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java
index a6c1ae81..d2f8c64b 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java
+++ b/jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java
@@ -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;
}
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 6ddd2cb6..249bc82b 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
@@ -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.
*
@@ -43,24 +43,23 @@ import javax.sql.DataSource;
@EnableJdbcRepositories
public class CategoryConfiguration {
-
@Bean
public ApplicationListener> loggingListener() {
return (ApplicationListener) event -> {
if (event instanceof JdbcEvent) {
- System.out.println("received an event: " + event);
+ System.out.println("Received an event: " + event);
}
};
}
-
@Bean
public ApplicationListener 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( //
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 eab90b1a..2f8e4cf2 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
@@ -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 {
-
-}
+interface CategoryRepository extends CrudRepository {}
diff --git a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/Output.java b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/Output.java
similarity index 87%
rename from jdbc/basics/src/main/java/example/springdata/jdbc/basics/Output.java
rename to jdbc/basics/src/test/java/example/springdata/jdbc/basics/Output.java
index a86d166a..8946d2bf 100644
--- a/jdbc/basics/src/main/java/example/springdata/jdbc/basics/Output.java
+++ b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/Output.java
@@ -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());
}
diff --git a/jdbc/basics/src/test/java/example/springdata/jdbc/basics/aggregate/AggregateTests.java b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/aggregate/AggregateTests.java
index 602f1895..f70c795f 100644
--- a/jdbc/basics/src/test/java/example/springdata/jdbc/basics/aggregate/AggregateTests.java
+++ b/jdbc/basics/src/test/java/example/springdata/jdbc/basics/aggregate/AggregateTests.java
@@ -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;
}
}
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 ad3fd4ad..a5c41f71 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
@@ -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.");
-
}
}
diff --git a/jdbc/pom.xml b/jdbc/pom.xml
index b211c0b8..745b43a2 100644
--- a/jdbc/pom.xml
+++ b/jdbc/pom.xml
@@ -1,43 +1,40 @@
- 4.0.0
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
- spring-data-jdbc-examples
- pom
+ spring-data-jdbc-examples
+ pom
-
- org.springframework.data.examples
- spring-data-examples
- 2.0.0.BUILD-SNAPSHOT
-
+
+ org.springframework.data.examples
+ spring-data-examples
+ 2.0.0.BUILD-SNAPSHOT
+
- Spring Data JDBC - Examples
- Sample projects for Spring Data JDBC
- http://projects.spring.io/spring-data-jdbc
- 2017
+ Spring Data JDBC - Examples
+ Sample projects for Spring Data JDBC
+ http://projects.spring.io/spring-data-jdbc
+ 2017
-
- basics
-
+
+ basics
+
-
+
+ Lovelace-BUILD-SNAPSHOT
+
-
- org.springframework.data
- spring-data-jdbc
- 1.0.0.BUILD-SNAPSHOT
-
+
-
- org.springframework.data
- spring-data-commons
- 2.1.0.BUILD-SNAPSHOT
-
+
+ org.springframework.data
+ spring-data-jdbc
+
-
- org.hsqldb
- hsqldb
-
-
+
+ org.hsqldb
+ hsqldb
+
+