From 2bc0de92d1d6a553b3f4c0bb5f5f94a7b74a8a4a Mon Sep 17 00:00:00 2001 From: "Michael J. Simons" Date: Mon, 5 Feb 2018 10:11:35 +0100 Subject: [PATCH] Move default implementation to NamingStrategy This makes it nicer to overwrite certain aspects with a lambda instead of an anonymous class. Brings the naming strategy in line with pairs like WebMvcConfigurer / WebMvcConfigurerAdapter. --- pom.xml | 24 +++++------ .../mapping/model/DefaultNamingStrategy.java | 37 +---------------- .../jdbc/mapping/model/NamingStrategy.java | 41 ++++++++++++++++--- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/pom.xml b/pom.xml index d4a06d3aef..8362725c30 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ 4.0.0 - org.springframework.data - spring-data-jdbc - 1.0.0.BUILD-SNAPSHOT + org.springframework.data + spring-data-jdbc + 1.0.0.BUILD-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. @@ -26,15 +26,15 @@ spring.data.jdbc reuseReports - 3.6.2 - 0.1.4 - 2.2.8 - 3.4.4 - 1.3.1 - 1.5.1 - 5.1.41 - 42.0.0 - + 3.6.2 + 0.1.4 + 2.2.8 + 3.4.4 + 1.3.1 + 1.5.1 + 5.1.41 + 42.0.0 + diff --git a/src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java b/src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java index c0f9b21625..d5acab9f06 100644 --- a/src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java @@ -23,41 +23,8 @@ * a different strategy on the fly. * * @author Greg Turnquist + * @author Michael Simons + * @deprecated Use {@link NamingStrategy} for a default implementation and implement methods as needed */ public class DefaultNamingStrategy implements NamingStrategy { - - /** - * No schema at all! - */ - @Override - public String getSchema() { - return ""; - } - - /** - * Look up the {@link Class}'s simple name. - */ - @Override - public String getTableName(Class type) { - return type.getSimpleName(); - } - - - /** - * Look up the {@link JdbcPersistentProperty}'s name. - */ - @Override - public String getColumnName(JdbcPersistentProperty property) { - return property.getName(); - } - - @Override - public String getReverseColumnName(JdbcPersistentProperty property) { - return property.getOwner().getTableName(); - } - - @Override - public String getKeyColumn(JdbcPersistentProperty property) { - return getReverseColumnName(property) + "_key"; - } } diff --git a/src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java b/src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java index c531c7e63e..49d0b51768 100644 --- a/src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java @@ -16,31 +16,60 @@ package org.springframework.data.jdbc.mapping.model; /** + * Interface and default implementation of a naming strategy. Defaults to no schema, + * table name based on {@link Class} and column name based on {@link JdbcPersistentProperty}. + * + * NOTE: Can also be used as an adapter. Create a lambda or an anonymous subclass and + * override any settings to implement a different strategy on the fly. + * * @author Greg Turnquist + * @author Michael Simons */ public interface NamingStrategy { - String getSchema(); + /** + * Defaults to no schema. + * + * @return No schema + */ + default String getSchema() { + return ""; + } - String getTableName(Class type); + /** + * Look up the {@link Class}'s simple name. + */ + default String getTableName(Class type) { + return type.getSimpleName(); + } - String getColumnName(JdbcPersistentProperty property); + /** + * Look up the {@link JdbcPersistentProperty}'s name. + */ + default String getColumnName(JdbcPersistentProperty property) { + return property.getName(); + } default String getQualifiedTableName(Class type) { return this.getSchema() + (this.getSchema().equals("") ? "" : ".") + this.getTableName(type); } /** - * For a reference A -> B this is the name in the table for B which references A. + * For a reference A -> B this is the name in the table for B which references A. * + * @param property The property who's column name in the owner table is required * @return a column name. */ - String getReverseColumnName(JdbcPersistentProperty property); + default String getReverseColumnName(JdbcPersistentProperty property) { + return property.getOwner().getTableName(); + } /** * For a map valued reference A -> Map>X,B< this is the name of the column in the tabel for B holding the key of the map. * @return */ - String getKeyColumn(JdbcPersistentProperty property); + default String getKeyColumn(JdbcPersistentProperty property){ + return getReverseColumnName(property) + "_key"; + } }