1717
1818import java .util .Optional ;
1919
20+ import org .jetbrains .annotations .NotNull ;
2021import org .springframework .data .mapping .model .BasicPersistentEntity ;
2122import org .springframework .data .relational .core .sql .SqlIdentifier ;
2223import org .springframework .data .util .Lazy ;
2930 * @author Jens Schauder
3031 * @author Greg Turnquist
3132 * @author Bastian Wilhelm
33+ * @author Mikhail Polivakha
3234 */
3335class RelationalPersistentEntityImpl <T > extends BasicPersistentEntity <T , RelationalPersistentProperty >
3436 implements RelationalPersistentEntity <T > {
3537
3638 private final NamingStrategy namingStrategy ;
3739 private final Lazy <Optional <SqlIdentifier >> tableName ;
40+ private final Lazy <Optional <SqlIdentifier >> schemaName ;
3841 private boolean forceQuote = true ;
3942
4043 /**
@@ -43,16 +46,20 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
4346 * @param information must not be {@literal null}.
4447 */
4548 RelationalPersistentEntityImpl (TypeInformation <T > information , NamingStrategy namingStrategy ) {
46-
4749 super (information );
50+ final Optional <Table > optionalTableAnnotation = Optional .ofNullable (findAnnotation (Table .class ));
4851
4952 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 )
5557 );
58+
59+ this .schemaName = Lazy .of (() -> optionalTableAnnotation
60+ .map (Table ::schema )
61+ .filter (StringUtils ::hasText )
62+ .map (this ::createSqlIdentifier ));
5663 }
5764
5865 private SqlIdentifier createSqlIdentifier (String name ) {
@@ -77,14 +84,30 @@ public void setForceQuote(boolean forceQuote) {
7784 */
7885 @ Override
7986 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+ }
8497
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 ();
88111 }
89112
90113 /*
0 commit comments