17
17
18
18
import java .util .Optional ;
19
19
20
- import org .jetbrains .annotations .NotNull ;
21
20
import org .springframework .data .mapping .model .BasicPersistentEntity ;
22
21
import org .springframework .data .relational .core .sql .SqlIdentifier ;
23
22
import org .springframework .data .util .Lazy ;
24
23
import org .springframework .data .util .TypeInformation ;
24
+ import org .springframework .lang .Nullable ;
25
25
import org .springframework .util .StringUtils ;
26
26
27
27
/**
@@ -46,17 +46,18 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
46
46
* @param information must not be {@literal null}.
47
47
*/
48
48
RelationalPersistentEntityImpl (TypeInformation <T > information , NamingStrategy namingStrategy ) {
49
+
49
50
super (information );
50
- final Optional <Table > optionalTableAnnotation = Optional .ofNullable (findAnnotation (Table .class ));
51
51
52
52
this .namingStrategy = namingStrategy ;
53
- this .tableName = Lazy .of (() -> optionalTableAnnotation
53
+
54
+ this .tableName = Lazy .of (() -> Optional .ofNullable (findAnnotation (Table .class ))
54
55
.map (Table ::value )
55
56
.filter (StringUtils ::hasText )
56
57
.map (this ::createSqlIdentifier )
57
58
);
58
59
59
- this .schemaName = Lazy .of (() -> optionalTableAnnotation
60
+ this .schemaName = Lazy .of (() -> Optional . ofNullable ( findAnnotation ( Table . class ))
60
61
.map (Table ::schema )
61
62
.filter (StringUtils ::hasText )
62
63
.map (this ::createSqlIdentifier ));
@@ -84,30 +85,33 @@ public void setForceQuote(boolean forceQuote) {
84
85
*/
85
86
@ Override
86
87
public SqlIdentifier getTableName () {
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 ())) );
88
+
89
+ SqlIdentifier schema = determineCurrentEntitySchema ();
90
+ Optional < SqlIdentifier > explicitlySpecifiedTableName = tableName . get ();
91
+
92
+ final SqlIdentifier schemalessTableIdentifier = createDerivedSqlIdentifier ( namingStrategy . getTableName ( getType ()));
93
+
94
+ if ( schema == null ) {
95
+ return explicitlySpecifiedTableName .orElse (schemalessTableIdentifier );
95
96
}
97
+
98
+ return explicitlySpecifiedTableName
99
+ .map (sqlIdentifier -> SqlIdentifier .from (schema , sqlIdentifier ))
100
+ .orElse (SqlIdentifier .from (schema , schemalessTableIdentifier ));
96
101
}
97
102
98
103
/**
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() }
104
+ * @return {@link SqlIdentifier} representing the current entity schema. If the schema is not specified, neither
105
+ * explicitly, nor via {@link NamingStrategy}, then return {@link null }
101
106
*/
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 ();
107
+ @ Nullable
108
+ private SqlIdentifier determineCurrentEntitySchema () {
109
+
110
+ Optional <SqlIdentifier > explicitlySpecifiedSchema = schemaName .get ();
111
+ return explicitlySpecifiedSchema .orElseGet (
112
+ () -> StringUtils .hasText (namingStrategy .getSchema ())
113
+ ? createDerivedSqlIdentifier (namingStrategy .getSchema ())
114
+ : null );
111
115
}
112
116
113
117
/*
0 commit comments