1. Introduction

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

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:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

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

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

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

3. Spring Boot Naming Strategies

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.

And, Spring Boot, provides defaults for both:

  • 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:

@Entity
public class Account {
    @Id 
    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.

We only need to indicate it in our properties file:

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

Or, expose them as @Beans:

@Bean
public PhysicalNamingStrategy physical() {
    return new PhysicalNamingStrategyStandardImpl();
}

@Bean
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.

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

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.

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

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are closed on this article!