Skip to content

Commit 9fc7482

Browse files
committed
Polishing.
Removed jetbrains annotation. Removed attempt to cache annotation lookup. Those lookups are already cached and obtaining them in the constructor causes overhead when they aren't requested at all. Limit the use of Optional. See #1099 Original pull request #1108
1 parent d12146d commit 9fc7482

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java

+27-23
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
import java.util.Optional;
1919

20-
import org.jetbrains.annotations.NotNull;
2120
import org.springframework.data.mapping.model.BasicPersistentEntity;
2221
import org.springframework.data.relational.core.sql.SqlIdentifier;
2322
import org.springframework.data.util.Lazy;
2423
import org.springframework.data.util.TypeInformation;
24+
import org.springframework.lang.Nullable;
2525
import 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
/*

spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImplUnitTests.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.springframework.data.relational.core.sql.SqlIdentifier.*;
2020

2121
import org.junit.jupiter.api.Test;
22-
2322
import org.springframework.data.annotation.Id;
2423
import org.springframework.data.relational.core.sql.IdentifierProcessing;
2524
import org.springframework.data.relational.core.sql.SqlIdentifier;
@@ -73,30 +72,34 @@ public void namingStrategyWithSchemaReturnsCompositeTableName() {
7372
.isEqualTo("\"MY_SCHEMA\".\"DUMMY_ENTITY_WITH_EMPTY_ANNOTATION\"");
7473
}
7574

76-
@Test // DATAJDBC-1099
75+
@Test // GH-1099
7776
void testRelationalPersistentEntitySchemaNameChoice() {
77+
7878
mappingContext = new RelationalMappingContext(NamingStrategyWithSchema.INSTANCE);
79-
final RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithExplicitSchema.class);
80-
final SqlIdentifier tableName = persistentEntity.getTableName();
79+
RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchemaAndName.class);
80+
81+
SqlIdentifier tableName = persistentEntity.getTableName();
82+
8183
assertThat(tableName).isEqualTo(SqlIdentifier.from(SqlIdentifier.quoted("DART_VADER"), quoted("I_AM_THE_SENATE")));
82-
assertThat(tableName.toString()).isEqualTo("\"DART_VADER\".\"I_AM_THE_SENATE\"");
8384
}
8485

85-
@Test // DATAJDBC-1099
86-
void testRelationalPersistentEntityTableOnlySchemaSpecified() {
87-
final RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchemaFromNamingStrategy.class);
88-
final SqlIdentifier tableName = persistentEntity.getTableName();
89-
assertThat(tableName).isEqualTo(SqlIdentifier.from(quoted("ANAKYN_SKYWALKER"), quoted("ENTITY_WITH_SCHEMA_FROM_NAMING_STRATEGY")));
90-
assertThat(tableName.toString()).isEqualTo("\"ANAKYN_SKYWALKER\".\"ENTITY_WITH_SCHEMA_FROM_NAMING_STRATEGY\"");
86+
@Test // GH-1099
87+
void specifiedSchemaGetsCombinedWithNameFromNamingStrategy() {
88+
89+
RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchema.class);
90+
91+
SqlIdentifier tableName = persistentEntity.getTableName();
92+
93+
assertThat(tableName).isEqualTo(SqlIdentifier.from(quoted("ANAKYN_SKYWALKER"), quoted("ENTITY_WITH_SCHEMA")));
9194
}
9295

9396
@Table(schema = "ANAKYN_SKYWALKER")
94-
static class EntityWithSchemaFromNamingStrategy {
97+
static class EntityWithSchema {
9598
@Id private Long id;
9699
}
97100

98101
@Table(schema = "DART_VADER", name = "I_AM_THE_SENATE")
99-
static class EntityWithExplicitSchema {
102+
static class EntityWithSchemaAndName {
100103
@Id private Long id;
101104
}
102105

0 commit comments

Comments
 (0)