Hibernate Field Naming with Spring Boot – 用Spring Boot命名Hibernate字段

最后修改: 2018年 11月 3日

1. Introduction


In this brief tutorial, we’ll see how to use the Hibernate naming strategies in a Spring Boot application.

在这个简短的教程中,我们将看到如何在Hibernate命名策略中使用Spring Boot应用程序。

2. Maven Dependencies


If we begin with a Maven-based Spring Boot application, and we are happy to embrace Spring Data, then we just need to add the Spring Data JPA dependency:

如果我们从基于Maven的Spring Boot应用程序开始,并且乐于接受Spring Data,那么我们只需添加Spring Data JPA依赖。


Also, we need a database, so we’ll use the in-memory database H2:



The Spring Data JPA dependency brings in the Hibernate dependencies for us.

Spring Data JPA的依赖性为我们带来了Hibernate的依赖性。

3. Spring Boot Naming Strategies

3.Spring Boot的命名策略

Hibernate maps field names using a physical strategy and an implicit strategy. We previously talked about how to work with the naming strategies in this tutorial.

Hibernate 使用物理策略隐式策略映射字段名。我们之前在这个教程中谈到了如何使用命名策略

And, Spring Boot, provides defaults for both:

而且,Spring Boot为这两者提供了默认值。

  • spring.jpa.hibernate.naming.physical-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy, and
  • spring.jpa.hibernate.naming.implicit-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

We can override these values, but by default, these will:


  • Replace dots with underscores
  • Change camel case to snake case, and
  • Lower-case table names

So, for example, an AddressBook entity would be created as the address_book table.


4. Strategies in Action


If we create an Account entity:


public class Account {
    private Long id;
    private String defaultEmail;

And then turn on some SQL debugging in our properties file:


hibernate.show_sql: true

At startup, we’ll see the following create statement in our logs:


Hibernate: create table account (id bigint not null, default_email varchar(255))

As we can see, the fields use snake case and are lowercased, following the Spring conventions.


And remember that we don’t need to specify the physical-strategy or the implicit-strategy properties in this case since we are accepting the defaults.


5. Customizing Strategies


So, let’s say that we want to use the strategies from JPA 1.0.

所以,假设我们想使用JPA 1.0的策略。

We only need to indicate it in our properties file:


        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

Or, expose them as @Beans:


public PhysicalNamingStrategy physical() {
    return new PhysicalNamingStrategyStandardImpl();

public ImplicitNamingStrategy implicit() {
    return new ImplicitNamingStrategyLegacyJpaImpl();

Either way, if we run our example with these changes, we’ll see a slightly different DDL statement:


Hibernate: create table Account (id bigint not null, defaultEmail varchar(255), primary key (id))

As we can see, this time these strategies follow the JPA 1.0’s naming convention.

我们可以看到,这一次这些策略遵循了JPA 1.0的命名惯例。

Now, if we wanted to use the JPA 2.0 naming rules we’d need to set ImplicitNamingStrategyJpaCompliantImpl as our implicit strategy.

现在,如果我们想使用JPA 2.0的命名规则,我们需要将ImplicitNamingStrategyJpaCompliantImpl作为我们的隐式策略。

6. Conclusion


In this tutorial, we saw how Spring Boot applies naming strategies to Hibernate’s query generator, and we also saw how to customize those strategies.

在本教程中,我们看到Spring Boot如何将命名策略应用于Hibernate的查询生成器,我们还看到如何定制这些策略。

And, as always, make sure check out all these samples over on GitHub.