|
16 | 16 | package org.springframework.data.jdbc.mapping.model;
|
17 | 17 |
|
18 | 18 | /**
|
| 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 | + * |
19 | 25 | * @author Greg Turnquist
|
| 26 | + * @author Michael Simons |
20 | 27 | */
|
21 | 28 | public interface NamingStrategy {
|
22 | 29 |
|
23 |
| - String getSchema(); |
| 30 | + /** |
| 31 | + * Defaults to no schema. |
| 32 | + * |
| 33 | + * @return No schema |
| 34 | + */ |
| 35 | + default String getSchema() { |
| 36 | + return ""; |
| 37 | + } |
24 | 38 |
|
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 | + } |
26 | 45 |
|
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 | + } |
28 | 52 |
|
29 | 53 | default String getQualifiedTableName(Class<?> type) {
|
30 | 54 | return this.getSchema() + (this.getSchema().equals("") ? "" : ".") + this.getTableName(type);
|
31 | 55 | }
|
32 | 56 |
|
33 | 57 | /**
|
34 |
| - * For a reference A -> B this is the name in the table for B which references A. |
| 58 | + * For a reference A -> B this is the name in the table for B which references A. |
35 | 59 | *
|
| 60 | + * @param property The property who's column name in the owner table is required |
36 | 61 | * @return a column name.
|
37 | 62 | */
|
38 |
| - String getReverseColumnName(JdbcPersistentProperty property); |
| 63 | + default String getReverseColumnName(JdbcPersistentProperty property) { |
| 64 | + return property.getOwner().getTableName(); |
| 65 | + } |
39 | 66 |
|
40 | 67 | /**
|
41 | 68 | * 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.
|
42 | 69 | * @return
|
43 | 70 | */
|
44 |
| - String getKeyColumn(JdbcPersistentProperty property); |
| 71 | + default String getKeyColumn(JdbcPersistentProperty property){ |
| 72 | + return getReverseColumnName(property) + "_key"; |
| 73 | + } |
45 | 74 |
|
46 | 75 | }
|
0 commit comments