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.
This commit is contained in:
committed by
Jens Schauder
parent
b1115833ab
commit
a6e4380789
@@ -23,41 +23,8 @@ package org.springframework.data.jdbc.mapping.model;
|
||||
* 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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user