Skip to content

Commit a6e4380

Browse files
michael-simonsschauder
authored andcommitted
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.
1 parent b111583 commit a6e4380

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,8 @@
2323
* a different strategy on the fly.
2424
*
2525
* @author Greg Turnquist
26+
* @author Michael Simons
27+
* @deprecated Use {@link NamingStrategy} for a default implementation and implement methods as needed
2628
*/
2729
public class DefaultNamingStrategy implements NamingStrategy {
28-
29-
/**
30-
* No schema at all!
31-
*/
32-
@Override
33-
public String getSchema() {
34-
return "";
35-
}
36-
37-
/**
38-
* Look up the {@link Class}'s simple name.
39-
*/
40-
@Override
41-
public String getTableName(Class<?> type) {
42-
return type.getSimpleName();
43-
}
44-
45-
46-
/**
47-
* Look up the {@link JdbcPersistentProperty}'s name.
48-
*/
49-
@Override
50-
public String getColumnName(JdbcPersistentProperty property) {
51-
return property.getName();
52-
}
53-
54-
@Override
55-
public String getReverseColumnName(JdbcPersistentProperty property) {
56-
return property.getOwner().getTableName();
57-
}
58-
59-
@Override
60-
public String getKeyColumn(JdbcPersistentProperty property) {
61-
return getReverseColumnName(property) + "_key";
62-
}
6330
}

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,60 @@
1616
package org.springframework.data.jdbc.mapping.model;
1717

1818
/**
19+
* Interface and default implementation of a naming strategy. Defaults to no schema,
20+
* table name based on {@link Class} and column name based on {@link JdbcPersistentProperty}.
21+
*
22+
* NOTE: Can also be used as an adapter. Create a lambda or an anonymous subclass and
23+
* override any settings to implement a different strategy on the fly.
24+
*
1925
* @author Greg Turnquist
26+
* @author Michael Simons
2027
*/
2128
public interface NamingStrategy {
2229

23-
String getSchema();
30+
/**
31+
* Defaults to no schema.
32+
*
33+
* @return No schema
34+
*/
35+
default String getSchema() {
36+
return "";
37+
}
2438

25-
String getTableName(Class<?> type);
39+
/**
40+
* Look up the {@link Class}'s simple name.
41+
*/
42+
default String getTableName(Class<?> type) {
43+
return type.getSimpleName();
44+
}
2645

27-
String getColumnName(JdbcPersistentProperty property);
46+
/**
47+
* Look up the {@link JdbcPersistentProperty}'s name.
48+
*/
49+
default String getColumnName(JdbcPersistentProperty property) {
50+
return property.getName();
51+
}
2852

2953
default String getQualifiedTableName(Class<?> type) {
3054
return this.getSchema() + (this.getSchema().equals("") ? "" : ".") + this.getTableName(type);
3155
}
3256

3357
/**
34-
* For a reference A -> B this is the name in the table for B which references A.
58+
* For a reference A -&gt; B this is the name in the table for B which references A.
3559
*
60+
* @param property The property who's column name in the owner table is required
3661
* @return a column name.
3762
*/
38-
String getReverseColumnName(JdbcPersistentProperty property);
63+
default String getReverseColumnName(JdbcPersistentProperty property) {
64+
return property.getOwner().getTableName();
65+
}
3966

4067
/**
4168
* 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.
4269
* @return
4370
*/
44-
String getKeyColumn(JdbcPersistentProperty property);
71+
default String getKeyColumn(JdbcPersistentProperty property){
72+
return getReverseColumnName(property) + "_key";
73+
}
4574

4675
}

0 commit comments

Comments
 (0)