17
17
18
18
import java .util .Optional ;
19
19
20
+ import org .jetbrains .annotations .NotNull ;
20
21
import org .springframework .data .mapping .model .BasicPersistentEntity ;
21
22
import org .springframework .data .relational .core .sql .SqlIdentifier ;
22
23
import org .springframework .data .util .Lazy ;
29
30
* @author Jens Schauder
30
31
* @author Greg Turnquist
31
32
* @author Bastian Wilhelm
33
+ * @author Mikhail Polivakha
32
34
*/
33
35
class RelationalPersistentEntityImpl <T > extends BasicPersistentEntity <T , RelationalPersistentProperty >
34
36
implements RelationalPersistentEntity <T > {
35
37
36
38
private final NamingStrategy namingStrategy ;
37
39
private final Lazy <Optional <SqlIdentifier >> tableName ;
40
+ private final Lazy <Optional <SqlIdentifier >> schemaName ;
38
41
private boolean forceQuote = true ;
39
42
40
43
/**
@@ -43,16 +46,20 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
43
46
* @param information must not be {@literal null}.
44
47
*/
45
48
RelationalPersistentEntityImpl (TypeInformation <T > information , NamingStrategy namingStrategy ) {
46
-
47
49
super (information );
50
+ final Optional <Table > optionalTableAnnotation = Optional .ofNullable (findAnnotation (Table .class ));
48
51
49
52
this .namingStrategy = namingStrategy ;
50
- this .tableName = Lazy .of (() -> Optional .ofNullable ( //
51
- findAnnotation (Table .class )) //
52
- .map (Table ::value ) //
53
- .filter (StringUtils ::hasText ) //
54
- .map (this ::createSqlIdentifier ) //
53
+ this .tableName = Lazy .of (() -> optionalTableAnnotation
54
+ .map (Table ::value )
55
+ .filter (StringUtils ::hasText )
56
+ .map (this ::createSqlIdentifier )
55
57
);
58
+
59
+ this .schemaName = Lazy .of (() -> optionalTableAnnotation
60
+ .map (Table ::schema )
61
+ .filter (StringUtils ::hasText )
62
+ .map (this ::createSqlIdentifier ));
56
63
}
57
64
58
65
private SqlIdentifier createSqlIdentifier (String name ) {
@@ -77,14 +84,30 @@ public void setForceQuote(boolean forceQuote) {
77
84
*/
78
85
@ Override
79
86
public SqlIdentifier getTableName () {
80
- return tableName .get ().orElseGet (() -> {
81
-
82
- String schema = namingStrategy .getSchema ();
83
- SqlIdentifier tableName = createDerivedSqlIdentifier (namingStrategy .getTableName (getType ()));
87
+ final Optional <SqlIdentifier > schema = determineCurrentEntitySchema ();
88
+ final Optional <SqlIdentifier > explicitlySpecifiedTableName = tableName .get ();
89
+ if (schema .isPresent ()) {
90
+ return explicitlySpecifiedTableName
91
+ .map (sqlIdentifier -> SqlIdentifier .from (schema .get (), sqlIdentifier ))
92
+ .orElse (SqlIdentifier .from (schema .get (), createDerivedSqlIdentifier (namingStrategy .getTableName (getType ()))));
93
+ } else {
94
+ return explicitlySpecifiedTableName .orElse (createDerivedSqlIdentifier (namingStrategy .getTableName (getType ())));
95
+ }
96
+ }
84
97
85
- return StringUtils .hasText (schema ) ? SqlIdentifier .from (createDerivedSqlIdentifier (schema ), tableName )
86
- : tableName ;
87
- });
98
+ /**
99
+ * @return Optional of {@link SqlIdentifier} representing the current entity schema. If the schema is not specified neither
100
+ * explicitly, nor via {@link NamingStrategy}, then return {@link Optional#empty()}
101
+ */
102
+ @ NotNull
103
+ private Optional <SqlIdentifier > determineCurrentEntitySchema () {
104
+ final Optional <SqlIdentifier > explicitlySpecifiedSchema = schemaName .get ();
105
+ if (explicitlySpecifiedSchema .isPresent ()) {
106
+ return explicitlySpecifiedSchema ;
107
+ }
108
+ return StringUtils .hasText (namingStrategy .getSchema ())
109
+ ? Optional .of (createDerivedSqlIdentifier (namingStrategy .getSchema ()))
110
+ : Optional .empty ();
88
111
}
89
112
90
113
/*
0 commit comments