DATAJDBC-106 - Support to specify a database object names using annotations.
@Table and @Column annotation added for configuring table and column names. Original pull request: #65.
This commit is contained in:
committed by
Jens Schauder
parent
9f2dfa69b8
commit
cd7403907a
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 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;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* The annotation to configure a mapping database column.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@Documented
|
||||
public @interface Column {
|
||||
|
||||
/**
|
||||
* The mapping column name.
|
||||
*/
|
||||
String value();
|
||||
|
||||
}
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
@@ -45,17 +49,23 @@ public interface NamingStrategy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the {@link Class}'s simple name.
|
||||
* Look up the {@link Class}'s simple name or {@link Table#value()}.
|
||||
*/
|
||||
default String getTableName(Class<?> type) {
|
||||
return type.getSimpleName();
|
||||
Table table = AnnotatedElementUtils.findMergedAnnotation(type, Table.class);
|
||||
return Optional.ofNullable(table)//
|
||||
.map(Table::value)//
|
||||
.orElse(type.getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the {@link JdbcPersistentProperty}'s name.
|
||||
* Look up the {@link JdbcPersistentProperty}'s name or {@link Column#value()}.
|
||||
*/
|
||||
default String getColumnName(JdbcPersistentProperty property) {
|
||||
return property.getName();
|
||||
Column column = property.findAnnotation(Column.class);
|
||||
return Optional.ofNullable(column)//
|
||||
.map(Column::value)//
|
||||
.orElse(property.getName());
|
||||
}
|
||||
|
||||
default String getQualifiedTableName(Class<?> type) {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 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;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* The annotation to configure a mapping database table.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface Table {
|
||||
|
||||
/**
|
||||
* The mapping table name.
|
||||
*/
|
||||
String value();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user