#411 - Simplified example by using @Column instead of NamingStrategy.
This commit is contained in:
@@ -19,8 +19,6 @@ import static java.util.Arrays.*;
|
||||
|
||||
import java.sql.Clob;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -31,8 +29,6 @@ import org.springframework.data.convert.CustomConversions;
|
||||
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.NamingStrategy;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
|
||||
import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -69,42 +65,6 @@ public class AggregateConfiguration extends JdbcConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NamingStrategy namingStrategy() {
|
||||
|
||||
Map<String, String> columnAliases = new HashMap<String, String>();
|
||||
columnAliases.put("lego_set.int_maximum_age", "max_age");
|
||||
columnAliases.put("lego_set.int_minimum_age", "min_age");
|
||||
|
||||
Map<String, String> reverseColumnAliases = new HashMap<String, String>();
|
||||
reverseColumnAliases.put("manual", "handbuch_id");
|
||||
|
||||
Map<String, String> keyColumnAliases = new HashMap<String, String>();
|
||||
keyColumnAliases.put("models", "name");
|
||||
|
||||
return new NamingStrategy() {
|
||||
|
||||
@Override
|
||||
public String getColumnName(RelationalPersistentProperty property) {
|
||||
|
||||
String defaultName = NamingStrategy.super.getColumnName(property);
|
||||
String key = getTableName(property.getOwner().getType()) + "." + defaultName;
|
||||
return columnAliases.computeIfAbsent(key, __ -> defaultName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReverseColumnName(RelationalPersistentProperty property) {
|
||||
return reverseColumnAliases.computeIfAbsent(property.getName(),
|
||||
__ -> NamingStrategy.super.getReverseColumnName(property));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKeyColumn(RelationalPersistentProperty property) {
|
||||
return keyColumnAliases.computeIfAbsent(property.getName(), __ -> NamingStrategy.super.getKeyColumn(property));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CustomConversions jdbcCustomConversions() {
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ 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 org.springframework.data.relational.core.mapping.Column;
|
||||
|
||||
/**
|
||||
* A Lego Set consisting of multiple Blocks and a manual
|
||||
@@ -48,9 +49,11 @@ public class LegoSet {
|
||||
* Since Manuals are part of a {@link LegoSet} and only make sense inside a {@link LegoSet} it is considered part of
|
||||
* the Aggregate.
|
||||
*/
|
||||
@Column("handbuch_id")
|
||||
private Manual manual;
|
||||
|
||||
// You can build multiple models from one LegoSet
|
||||
@Column(keyColumn = "name")
|
||||
private final @AccessType(Type.FIELD) @Wither(AccessLevel.PACKAGE) Map<String, Model> models;
|
||||
|
||||
LegoSet() {
|
||||
@@ -59,6 +62,7 @@ public class LegoSet {
|
||||
|
||||
// conversion for custom types currently has to be done through getters/setter + marking the underlying property with
|
||||
// @Transient.
|
||||
@Column("min_age")
|
||||
public int getIntMinimumAge() {
|
||||
return toInt(this.minimumAge);
|
||||
}
|
||||
@@ -67,6 +71,7 @@ public class LegoSet {
|
||||
minimumAge = toPeriod(years);
|
||||
}
|
||||
|
||||
@Column("max_age")
|
||||
public int getIntMaximumAge() {
|
||||
return toInt(this.maximumAge);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user