DATAJDBC-189 - Polishing.
Removed DefaultNamingStrategy since we don't have GA release breaking APIs is still ok. Introduced an instance of NamingStrategy so we don't have to create a new class whereever we just want the default implementation. JavaDoc. Formatting Original pull request: #36.
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
/**
|
||||
* Basic implementation of {@link NamingStrategy} with no schema, table based on {@link Class} and
|
||||
* column name based on {@link JdbcPersistentProperty}.
|
||||
*
|
||||
* NOTE: Can also be used as an adapter. Create an anonymous subclass and override any settings to implement
|
||||
* 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 {
|
||||
}
|
||||
@@ -22,14 +22,14 @@ import org.springframework.data.util.ParsingUtils;
|
||||
* column name based on {@link JdbcPersistentProperty}. The default delimiter is '_', resulting in snake case.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class DelimiterNamingStrategy extends DefaultNamingStrategy {
|
||||
public class DelimiterNamingStrategy implements NamingStrategy {
|
||||
|
||||
private final String delimiter;
|
||||
|
||||
/**
|
||||
* Construct a instance with '_' as delimiter.
|
||||
* This results in a snake case naming strategy.
|
||||
* Construct a instance with '_' as delimiter. This results in a snake case naming strategy.
|
||||
*/
|
||||
public DelimiterNamingStrategy() {
|
||||
this("_");
|
||||
@@ -49,7 +49,7 @@ public class DelimiterNamingStrategy extends DefaultNamingStrategy {
|
||||
*/
|
||||
@Override
|
||||
public String getTableName(Class<?> type) {
|
||||
return ParsingUtils.reconcatenateCamelCase(super.getTableName(type), delimiter);
|
||||
return ParsingUtils.reconcatenateCamelCase(NamingStrategy.super.getTableName(type), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ public class DelimiterNamingStrategy extends DefaultNamingStrategy {
|
||||
*/
|
||||
@Override
|
||||
public String getColumnName(JdbcPersistentProperty property) {
|
||||
return ParsingUtils.reconcatenateCamelCase(super.getColumnName(property), delimiter);
|
||||
return ParsingUtils.reconcatenateCamelCase(NamingStrategy.super.getColumnName(property), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,7 +71,7 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
|
||||
}
|
||||
|
||||
public JdbcMappingContext(NamedParameterJdbcOperations template) {
|
||||
this(new DefaultNamingStrategy(), template, __ -> {});
|
||||
this(NamingStrategy.INSTANCE, template, __ -> {});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,21 +16,28 @@
|
||||
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.
|
||||
* 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}.
|
||||
* <p>
|
||||
* 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 {
|
||||
|
||||
/**
|
||||
* Empty implementation of the interface utilizing only the default implementation.
|
||||
* <p>
|
||||
* Using this avoids creating essentially the same class over and over again.
|
||||
*/
|
||||
NamingStrategy INSTANCE = new NamingStrategy() {};
|
||||
|
||||
/**
|
||||
* Defaults to no schema.
|
||||
*
|
||||
* @return No schema
|
||||
* @return Empty String representing no schema
|
||||
*/
|
||||
default String getSchema() {
|
||||
return "";
|
||||
@@ -58,17 +65,19 @@ public interface NamingStrategy {
|
||||
* 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.
|
||||
* @return a column name. Must not be {@code null}.
|
||||
*/
|
||||
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
|
||||
* For a map valued reference A -> Map>X,B< this is the name of the column in the table for B holding the key of
|
||||
* the map.
|
||||
*
|
||||
* @return name of the key column. Must not be {@code null}.
|
||||
*/
|
||||
default String getKeyColumn(JdbcPersistentProperty property){
|
||||
default String getKeyColumn(JdbcPersistentProperty property) {
|
||||
return getReverseColumnName(property) + "_key";
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.util.Optional;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.NamingStrategy;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
@@ -35,9 +34,9 @@ public class JdbcConfiguration {
|
||||
|
||||
@Bean
|
||||
JdbcMappingContext jdbcMappingContext(NamedParameterJdbcTemplate template, Optional<NamingStrategy> namingStrategy,
|
||||
Optional<ConversionCustomizer> conversionCustomizer) {
|
||||
Optional<ConversionCustomizer> conversionCustomizer) {
|
||||
|
||||
return new JdbcMappingContext(
|
||||
namingStrategy.orElse(new DefaultNamingStrategy()), template, conversionCustomizer.orElse(conversionService -> {}));
|
||||
return new JdbcMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE), template,
|
||||
conversionCustomizer.orElse(conversionService -> {}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user