Browse Source

DATAJDBC-296 - The value of the `@Table` annotation is now optional.

The default ("") is equivalent to no annotation at all, i.e. the `NamingStrategy` determines the table name.

Original pull request: #109.
pull/128/head
Bastian Wilhelm 7 years ago committed by Jens Schauder
parent
commit
807688e63c
  1. 8
      spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java
  2. 3
      spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Table.java
  3. 52
      spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityWithoutNamesImplUnitTests.java

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

@ -21,12 +21,14 @@ import org.springframework.data.mapping.model.BasicPersistentEntity; @@ -21,12 +21,14 @@ import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.PersistentPropertyAccessorFactory;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.StringUtils;
/**
* Meta data a repository might need for implementing persistence operations for instances of type {@code T}
*
* @author Jens Schauder
* @author Greg Turnquist
* @author Bastian Wilhelm
*/
class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, RelationalPersistentProperty>
implements RelationalPersistentEntity<T> {
@ -44,7 +46,11 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio @@ -44,7 +46,11 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
super(information);
this.namingStrategy = namingStrategy;
this.tableName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Table.class)).map(Table::value));
this.tableName = Lazy.of(() -> Optional.ofNullable( //
findAnnotation(Table.class)) //
.map(Table::value) //
.filter(StringUtils::hasText) //
);
}
/*

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

@ -26,6 +26,7 @@ import java.lang.annotation.Target; @@ -26,6 +26,7 @@ import java.lang.annotation.Target;
* The annotation to configure the mapping from a class to a database table.
*
* @author Kazuki Shimizu
* @author Bastian Wilhelm
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ -36,5 +37,5 @@ public @interface Table { @@ -36,5 +37,5 @@ public @interface Table {
/**
* The mapping table name.
*/
String value();
String value() default "";
}

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

@ -0,0 +1,52 @@ @@ -0,0 +1,52 @@
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link RelationalPersistentEntityImpl} without names in {@link Column} and {@link Table}.
*
* @author Bastian Wilhelm
*/
public class RelationalPersistentEntityWithoutNamesImplUnitTests {
RelationalMappingContext mappingContext = new RelationalMappingContext();
@Test // DATAJDBC-296
public void discoversAnnotatedTableName() {
RelationalPersistentEntity<?> entity = mappingContext.getPersistentEntity(DummySubEntity.class);
assertThat(entity.getTableName()).isEqualTo("dummy_sub_entity");
}
@Test // DATAJDBC-296
public void considerIdColumnName() {
RelationalPersistentEntity<?> entity = mappingContext.getPersistentEntity(DummySubEntity.class);
assertThat(entity.getIdColumn()).isEqualTo("id");
}
@Table()
static class DummySubEntity {
@Id @Column() Long id;
}
}
Loading…
Cancel
Save