Skip to content

Commit

Permalink
spring-projects#313 - Polishing.
Browse files Browse the repository at this point in the history
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: spring-projects#324.
  • Loading branch information
odrotbohm committed Feb 22, 2018
1 parent 5d556d6 commit 74d947e
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 140 deletions.
1 change: 1 addition & 0 deletions jdbc/basics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

<name>Spring Data JDBC - Basic usage examples</name>
<description>Sample project demonstrating Spring Data JDBC features</description>

</project>
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -24,21 +32,16 @@
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
*/
Expand All @@ -54,14 +57,17 @@ public ApplicationListener<?> idSetting() {
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());
}
Expand Down Expand Up @@ -98,8 +104,7 @@ public String getColumnName(JdbcPersistentProperty property) {

@Override
public String getTableName(Class<?> type) {

return tableAliases.computeIfAbsent(super.getTableName(type),key -> key);
return tableAliases.computeIfAbsent(super.getTableName(type), key -> key);
}

@Override
Expand Down Expand Up @@ -128,6 +133,7 @@ public String convert(Clob clob) {
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);
}
Expand All @@ -140,7 +146,6 @@ public String convert(Clob clob) {
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {

NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);

DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();

accessStrategy.setDelegate(new DefaultDataAccessStrategy( //
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
*
Expand All @@ -35,25 +36,21 @@
@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);
}
Expand All @@ -70,7 +67,6 @@ public void setIntMaximumAge(int years) {
maximumAge = toPeriod(years);
}


private static int toInt(Period period) {
return (int) (period == null ? 0 : period.get(ChronoUnit.YEARS));
}
Expand All @@ -84,6 +80,7 @@ public void addModel(String name, String description) {
Model model = new Model();
model.name = name;
model.description = description;

models.put(name, model);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,8 +19,7 @@

/**
* A repository for {@link LegoSet}.
*
* @author Jens Schauder
*/
public interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {

}
interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}.
*
Expand All @@ -27,17 +27,13 @@
@Data
public class Manual {

private @Id Long id;
private String author, text;

Manual(String text, String author) {

this.id = null;
this.author = author;
this.text = text;
}

@Id
private Long id;

private String author;
private String text;
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
*
Expand All @@ -32,6 +32,12 @@
@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;
Expand All @@ -40,23 +46,10 @@ public Category(String name, String description, AgeGroup ageGroup) {
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;
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -30,8 +32,6 @@
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>
Expand All @@ -43,24 +43,23 @@
@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();
Expand All @@ -73,7 +72,6 @@ public ApplicationListener<BeforeSave> timeStampingSaveTime() {
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {

NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);

DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();

accessStrategy.setDelegate(new DefaultDataAccessStrategy( //
Expand Down
Loading

0 comments on commit 74d947e

Please sign in to comment.