Skip to content

Commit 3edab4f

Browse files
committed
DATAJDBC-106 - Support to specify a database object name using annotation
* Added @table * Added @column
1 parent 41e31b1 commit 3edab4f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.model;
1717

18+
import org.springframework.core.annotation.AnnotatedElementUtils;
19+
20+
import java.util.Optional;
21+
1822
/**
1923
* Interface and default implementation of a naming strategy. Defaults to no schema, table name based on {@link Class}
2024
* and column name based on {@link JdbcPersistentProperty}.
@@ -45,17 +49,23 @@ default String getSchema() {
4549
}
4650

4751
/**
48-
* Look up the {@link Class}'s simple name.
52+
* Look up the {@link Class}'s simple name or {@link Table#value()}.
4953
*/
5054
default String getTableName(Class<?> type) {
51-
return type.getSimpleName();
55+
Table table = AnnotatedElementUtils.findMergedAnnotation(type, Table.class);
56+
return Optional.ofNullable(table)//
57+
.map(Table::value)//
58+
.orElse(type.getSimpleName());
5259
}
5360

5461
/**
55-
* Look up the {@link JdbcPersistentProperty}'s name.
62+
* Look up the {@link JdbcPersistentProperty}'s name or {@link Column#value()}.
5663
*/
5764
default String getColumnName(JdbcPersistentProperty property) {
58-
return property.getName();
65+
Column column = property.findAnnotation(Column.class);
66+
return Optional.ofNullable(column)//
67+
.map(Column::value)//
68+
.orElse(property.getName());
5969
}
6070

6171
default String getQualifiedTableName(Class<?> type) {

0 commit comments

Comments
 (0)