Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – JPA Buddy – JPA (partner)
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Persistence footer banner
Comments are closed on this article!