diff --git a/spring-boot/src/main/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java b/spring-boot/src/main/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java index 1deea39b65..281a8eabc6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java +++ b/spring-boot/src/main/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java @@ -27,6 +27,7 @@ import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; * conventions. * * @author Phillip Webb + * @author Madhura Bhave * @since 1.4.0 */ public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy { @@ -34,45 +35,59 @@ public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy { @Override public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) { - return apply(name); + return apply(name, jdbcEnvironment); } @Override public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) { - return apply(name); + return apply(name, jdbcEnvironment); } @Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) { - return apply(name); + return apply(name, jdbcEnvironment); } @Override public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) { - return apply(name); + return apply(name, jdbcEnvironment); } @Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) { - return apply(name); + return apply(name, jdbcEnvironment); } - private Identifier apply(Identifier name) { + private Identifier apply(Identifier name, JdbcEnvironment jdbcEnvironment) { if (name == null) { return null; } - StringBuilder text = new StringBuilder(name.getText().replace('.', '_')); - for (int i = 1; i < text.length() - 1; i++) { - if (isUnderscoreRequired(text.charAt(i - 1), text.charAt(i), - text.charAt(i + 1))) { - text.insert(i++, '_'); + StringBuilder builder = new StringBuilder(name.getText().replace('.', '_')); + for (int i = 1; i < builder.length() - 1; i++) { + if (isUnderscoreRequired(builder.charAt(i - 1), builder.charAt(i), + builder.charAt(i + 1))) { + builder.insert(i++, '_'); } } - return new Identifier(text.toString().toLowerCase(Locale.ROOT), name.isQuoted()); + + String text = builder.toString(); + String finalText = isCaseInsensitive(jdbcEnvironment) ? text.toLowerCase(Locale.ROOT) + : text; + return new Identifier(finalText, name.isQuoted()); + } + + /** + * Specify whether the database is case sensitive. + * @param jdbcEnvironment The JDBC environment which can be used to determine case + * @return true if the database is case insensitive + * sensitivity + */ + protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) { + return true; } private boolean isUnderscoreRequired(char before, char current, char after) { diff --git a/spring-boot/src/test/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java b/spring-boot/src/test/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java index 0ce35b59a2..39353f2103 100644 --- a/spring-boot/src/test/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java @@ -23,6 +23,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.mapping.PersistentClass; import org.hibernate.service.ServiceRegistry; import org.junit.Before; @@ -34,17 +35,22 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link SpringPhysicalNamingStrategy}. * * @author Phillip Webb + * @author Madhura Bhave */ public class SpringPhysicalNamingStrategyTests { private Metadata metadata; + private MetadataSources metadataSources; + + private StandardServiceRegistry serviceRegistry; + @Before public void setup() throws Exception { - MetadataSources metadataSources = new MetadataSources(); - metadataSources.addAnnotatedClass(TelephoneNumber.class); - StandardServiceRegistry serviceRegistry = getServiceRegistry(metadataSources); - this.metadata = metadataSources.getMetadataBuilder(serviceRegistry) + this.metadataSources = new MetadataSources(); + this.metadataSources.addAnnotatedClass(TelephoneNumber.class); + this.serviceRegistry = getServiceRegistry(this.metadataSources); + this.metadata = this.metadataSources.getMetadataBuilder(this.serviceRegistry) .applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy()).build(); } @@ -61,4 +67,21 @@ public class SpringPhysicalNamingStrategyTests { assertThat(binding.getTable().getQuotedName()).isEqualTo("telephone_number"); } + @Test + public void tableNameShouldNotBeLowerCaseIfCaseSensitive() throws Exception { + this.metadata = this.metadataSources.getMetadataBuilder(this.serviceRegistry) + .applyPhysicalNamingStrategy(new TestSpringPhysicalNamingStrategy()).build(); + PersistentClass binding = this.metadata + .getEntityBinding(TelephoneNumber.class.getName()); + assertThat(binding.getTable().getQuotedName()).isEqualTo("Telephone_Number"); + } + + private class TestSpringPhysicalNamingStrategy extends SpringPhysicalNamingStrategy { + + @Override + protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) { + return false; + } + + } }