Commit 312535bc authored by Phillip Webb's avatar Phillip Webb

Add SpringNamingStrategy to improve FK names

Add a new `SpringNamingStrategy` hibernate `NamingStrategy` that
extends `ImprovedNamingStrategy` to improve the name of foreign
key columns.

Fixes gh-213
parent 28a92d22
......@@ -21,7 +21,6 @@ import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.ejb.HibernateEntityManager;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
......@@ -31,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.orm.jpa.SpringNamingStrategy;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
......@@ -83,7 +83,7 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implemen
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
properties.put("hibernate.ejb.naming_strategy", this.environment.getProperty(
"naming-strategy", ImprovedNamingStrategy.class.getName()));
"naming-strategy", SpringNamingStrategy.class.getName()));
String ddlAuto = this.environment.getProperty("ddl-auto", getDefaultDdlAuto());
if (!"none".equals(ddlAuto)) {
properties.put("hibernate.hbm2ddl.auto", ddlAuto);
......
......@@ -64,6 +64,11 @@
<artifactId>jetty-util</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
......
/*
* Copyright 2012-2014 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;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.util.StringHelper;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Hibernate {@link NamingStrategy} that follows Spring recommended naming conventions.
* Naming conventions implemented here are identical to {@link ImprovedNamingStrategy}
* with the exception that foreign key columns include the referenced column name.
*
* @author Phillip Webb
* @see "http://stackoverflow.com/questions/7689206/ejb3namingstrategy-vs-improvednamingstrategy-foreign-key-naming"
*/
public class SpringNamingStrategy extends ImprovedNamingStrategy {
@Override
public String foreignKeyColumnName(String propertyName, String propertyEntityName,
String propertyTableName, String referencedColumnName) {
String name = propertyTableName;
if (propertyName != null) {
name = StringHelper.unqualify(propertyName);
}
Assert.state(StringUtils.hasLength(name),
"Unable to generate foreignKeyColumnName");
return columnName(name) + "_" + referencedColumnName;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment