Align Hibernate 5 join table names with those from SpringNamingStrategy

Previously, the name of a join table when using Hibernate 5 would
differ from those when using Hibernate 4 with the default
SpringNamingStrategy.

This commit introduces SpringImplicitNamingStrategy which customises the
name of join tables to match those produced by SpringNamingStrategy.

Closes gh-5880
pull/5889/merge
Andy Wilkinson 9 years ago
parent 1bb2c4fc00
commit 5c0d400c23

@ -235,6 +235,8 @@ public class JpaProperties {
private static final String DEFAULT_PHYSICAL_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy";
private static final String DEFAULT_IMPLICIT_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy";
/**
* Hibernate 5 implicit naming strategy fully qualified name.
*/
@ -288,7 +290,8 @@ public class JpaProperties {
private void applyHibernate5NamingStrategy(Map<String, String> properties) {
applyHibernate5NamingStrategy(properties,
"hibernate.implicit_naming_strategy", this.implicitStrategy, null);
"hibernate.implicit_naming_strategy", this.implicitStrategy,
DEFAULT_IMPLICIT_STRATEGY);
applyHibernate5NamingStrategy(properties,
"hibernate.physical_naming_strategy", this.physicalStrategy,
DEFAULT_PHYSICAL_STRATEGY);

@ -26,6 +26,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.boot.test.util.EnvironmentTestUtils;
@ -98,11 +99,14 @@ public class JpaPropertiesTests {
JpaProperties properties = load(HibernateVersion.V5);
Map<String, String> hibernateProperties = properties
.getHibernateProperties(mockStandaloneDataSource());
assertThat(hibernateProperties).doesNotContainKeys(
"hibernate.ejb.naming_strategy", "hibernate.implicit_naming_strategy");
assertThat(hibernateProperties)
.doesNotContainKeys("hibernate.ejb.naming_strategy");
assertThat(hibernateProperties).containsEntry(
"hibernate.physical_naming_strategy",
SpringPhysicalNamingStrategy.class.getName());
assertThat(hibernateProperties).containsEntry(
"hibernate.implicit_naming_strategy",
SpringImplicitNamingStrategy.class.getName());
}
@Test

@ -28,8 +28,8 @@
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>

@ -7,11 +7,11 @@ insert into note(title, body) values ('Spring Framework', 'Core support for depe
insert into note(title, body) values ('Spring Integration', 'Extends the Spring programming model to support the well-known Enterprise Integration Patterns.')
insert into note(title, body) values ('Tomcat', 'Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies.')
insert into note_tag(notes_id, tags_id) values (1, 1)
insert into note_tag(notes_id, tags_id) values (2, 1)
insert into note_tag(notes_id, tags_id) values (3, 1)
insert into note_tag(notes_id, tags_id) values (1, 3)
insert into note_tag(notes_id, tags_id) values (2, 3)
insert into note_tag(notes_id, tags_id) values (3, 3)
insert into note_tag(notes_id, tags_id) values (4, 2)
insert into note_tag(notes_id, tags_id) values (4, 3)
insert into note_tags(notes_id, tags_id) values (1, 1)
insert into note_tags(notes_id, tags_id) values (2, 1)
insert into note_tags(notes_id, tags_id) values (3, 1)
insert into note_tags(notes_id, tags_id) values (1, 3)
insert into note_tags(notes_id, tags_id) values (2, 3)
insert into note_tags(notes_id, tags_id) values (3, 3)
insert into note_tags(notes_id, tags_id) values (4, 2)
insert into note_tags(notes_id, tags_id) values (4, 3)

@ -0,0 +1,43 @@
/*
* Copyright 2012-2016 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.boot.orm.jpa.hibernate;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitJoinTableNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
/**
* Hibernate {@link ImplicitNamingStrategy} that follows Spring recommended naming
* conventions. Naming conventions implemented here are identical to
* {@link ImplicitNamingStrategyJpaCompliantImpl} with the exception that join table names
* are of the form
* <code>{owning_physical_table_name}_{association_owning_property_name}</code>.
*
* @author Andy Wilkinson
* @since 1.4.0
*/
public class SpringImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl {
@Override
public Identifier determineJoinTableName(ImplicitJoinTableNameSource source) {
String name = source.getOwningPhysicalTableName() + "_"
+ source.getAssociationOwningAttributePath().getProperty();
return toIdentifier(name, source.getBuildingContext());
}
}
Loading…
Cancel
Save