1717
1818import java .util .Optional ;
1919
20- import org .jetbrains .annotations .NotNull ;
2120import org .springframework .data .mapping .model .BasicPersistentEntity ;
2221import org .springframework .data .relational .core .sql .SqlIdentifier ;
2322import org .springframework .data .util .Lazy ;
2423import org .springframework .data .util .TypeInformation ;
24+ import org .springframework .lang .Nullable ;
2525import org .springframework .util .StringUtils ;
2626
2727/**
@@ -46,17 +46,18 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
4646 * @param information must not be {@literal null}.
4747 */
4848 RelationalPersistentEntityImpl (TypeInformation <T > information , NamingStrategy namingStrategy ) {
49+
4950 super (information );
50- final Optional <Table > optionalTableAnnotation = Optional .ofNullable (findAnnotation (Table .class ));
5151
5252 this .namingStrategy = namingStrategy ;
53- this .tableName = Lazy .of (() -> optionalTableAnnotation
53+
54+ this .tableName = Lazy .of (() -> Optional .ofNullable (findAnnotation (Table .class ))
5455 .map (Table ::value )
5556 .filter (StringUtils ::hasText )
5657 .map (this ::createSqlIdentifier )
5758 );
5859
59- this .schemaName = Lazy .of (() -> optionalTableAnnotation
60+ this .schemaName = Lazy .of (() -> Optional . ofNullable ( findAnnotation ( Table . class ))
6061 .map (Table ::schema )
6162 .filter (StringUtils ::hasText )
6263 .map (this ::createSqlIdentifier ));
@@ -84,30 +85,33 @@ public void setForceQuote(boolean forceQuote) {
8485 */
8586 @ Override
8687 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 );
9596 }
97+
98+ return explicitlySpecifiedTableName
99+ .map (sqlIdentifier -> SqlIdentifier .from (schema , sqlIdentifier ))
100+ .orElse (SqlIdentifier .from (schema , schemalessTableIdentifier ));
96101 }
97102
98103 /**
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 }
101106 */
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 );
111115 }
112116
113117 /*
0 commit comments