Browse Source

DATAJDBC-189 - 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.

Original pull request: #36.
pull/50/merge
Michael J. Simons 8 years ago committed by Jens Schauder
parent
commit
a6e4380789
  1. 37
      src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java
  2. 41
      src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java

37
src/main/java/org/springframework/data/jdbc/mapping/model/DefaultNamingStrategy.java

@ -23,41 +23,8 @@ package org.springframework.data.jdbc.mapping.model;
* a different strategy on the fly. * a different strategy on the fly.
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Michael Simons
* @deprecated Use {@link NamingStrategy} for a default implementation and implement methods as needed
*/ */
public class DefaultNamingStrategy implements NamingStrategy { 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";
}
} }

41
src/main/java/org/springframework/data/jdbc/mapping/model/NamingStrategy.java

@ -16,31 +16,60 @@
package org.springframework.data.jdbc.mapping.model; 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 Greg Turnquist
* @author Michael Simons
*/ */
public interface NamingStrategy { 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) { default String getQualifiedTableName(Class<?> type) {
return this.getSchema() + (this.getSchema().equals("") ? "" : ".") + this.getTableName(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 -&gt; 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. * @return a column name.
*/ */
String getReverseColumnName(JdbcPersistentProperty property); default String getReverseColumnName(JdbcPersistentProperty property) {
return property.getOwner().getTableName();
}
/** /**
* For a map valued reference A -> Map&gt;X,B&lt; this is the name of the column in the tabel for B holding the key of the map. * For a map valued reference A -> Map&gt;X,B&lt; this is the name of the column in the tabel for B holding the key of the map.
* @return * @return
*/ */
String getKeyColumn(JdbcPersistentProperty property); default String getKeyColumn(JdbcPersistentProperty property){
return getReverseColumnName(property) + "_key";
}
} }

Loading…
Cancel
Save