How to map camelCase properties to snake_case column names with Hibernate

Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome?

Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework.

So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night!

Introduction

In this article, you are going to learn how to map cameCase entity properties (e.g., phoneNumber) to snake_case column names (e.g., phone_number) using a Hibernate naming strategy.

While you could achieve the same goal with the name attribute of JPA @Column annotation, it’s much more convenient to use a custom Hibernate strategy to apply this naming convention consistently.

Domain Model

Let’s assume we are using the following BookAuthor and PaperBackBook entities in our application:

How to map camelCase properties to snake_case column names with Hibernate

The JPA entities are mapped like this:

@Entity(name = "BookAuthor")
public class BookAuthor {

    @Id
    private Long id;

    private String firstName;

    private String lastName;

    //Getters and setters omitted for brevity
}

@Entity(name = "PaperBackBook")
public class PaperBackBook {

    @Id
    @GeneratedValue(
        strategy = GenerationType.SEQUENCE
    )
    private Long id;

    @NaturalId
    private String ISBN;

    private String title;

    private LocalDate publishedOn;

    @ManyToOne(fetch = FetchType.LAZY)
    private BookAuthor publishedBy;

    //Getters and setters omitted for brevity
}

The @ManyToOne association uses lazy loading because the default EAGER fetching strategy is almost always a bad idea.

Database schema associated with the default JPA mapping

If we generate the database schema using the hbm2dll tool, the following DDL statements are going to be executed:

CREATE SEQUENCE hibernate_sequence
START WITH 1 INCREMENT BY 1

CREATE TABLE BookAuthor (
    id          BIGINT NOT NULL,
    firstName   VARCHAR(255),
    lastName    VARCHAR(255),
    PRIMARY KEY (id)
)

CREATE TABLE PaperBackBook (
    id              BIGINT NOT NULL,
    ISBN            VARCHAR(255),
    publishedOn     DATE, 
    title           VARCHAR(255),
    publishedBy_id  BIGINT, 
    PRIMARY KEY (id)
)

By default, Hibernate assumes the entity class name as well as the property names when mapping the JPA entities to the underlying database tables. However, while the cameCase naming convention is fine for Java code, we want to use the snake_case naming convention for the database schema. Luckily, Hibernate is very extensible so we can achieve this goal using a custom naming strategy.

Naming strategy

Since Hibernate 5, the database object naming strategy is represented by the PhysicalNamingStrategy interface which we can customize to automatically transform database identifiers from cameCase to snake_case.

public class CamelCaseToSnakeCaseNamingStrategy 
        extends PhysicalNamingStrategyStandardImpl {

    public static final CamelCaseToSnakeCaseNamingStrategy INSTANCE = 
        new CamelCaseToSnakeCaseNamingStrategy();

    public static final String CAMEL_CASE_REGEX = "([a-z]+)([A-Z]+)";

    public static final String SNAKE_CASE_PATTERN = "$1\\_$2";

    @Override
    public Identifier toPhysicalCatalogName(
            Identifier name, 
            JdbcEnvironment context) {
        return formatIdentifier(
            super.toPhysicalCatalogName(name, context)
        );
    }

    @Override
    public Identifier toPhysicalSchemaName(
            Identifier name, 
            JdbcEnvironment context) {
        return formatIdentifier(
            super.toPhysicalSchemaName(name, context)
        );
    }

    @Override
    public Identifier toPhysicalTableName(
            Identifier name, 
            JdbcEnvironment context) {
        return formatIdentifier(
            super.toPhysicalTableName(name, context)
        );
    }

    @Override
    public Identifier toPhysicalSequenceName(
            Identifier name, 
            JdbcEnvironment context) {
        return formatIdentifier(
            super.toPhysicalSequenceName(name, context)
        );
    }

    @Override
    public Identifier toPhysicalColumnName(
            Identifier name, 
            JdbcEnvironment context) {
        return formatIdentifier(
            super.toPhysicalColumnName(name, context)
        );
    }

    private Identifier formatIdentifier(
            Identifier identifier) {
        if (identifier != null) {
            String name = identifier.getText();

        String formattedName = name
        .replaceAll(
            CAMEL_CASE_REGEX, 
            SNAKE_CASE_PATTERN)
        .toLowerCase();

        return !formattedName.equals(name) ?
                    Identifier.toIdentifier(
                        formattedName, 
                        identifier.isQuoted()
                    ) :
                    identifier;
        } else {
            return null;
        }

    }
}

You don’t even need to create the aforementioned naming strategy class. You can get it via the Hypersistence Utils Maven dependency:

<dependency>
    <groupId>io.hypersistence</groupId>
    <artifactId>hypersistence-utils-hibernate-60</artifactId>
    <version>${hypersistence-utils.version}</version>
</dependency>

In order to use the CamelCaseToSnakeCaseNamingStrategy custom naming strategy, you need to supply it to Hibernate via the hibernate.physical_naming_strategy configuration property.

If you’re using Spring Boot, you can do it like this:

spring.jpa.properties.hibernate.physical_naming_strategy=io.hypersistence.utils.hibernate.naming.CamelCaseToSnakeCaseNamingStrategy

If you’re using Java EE or Jakarta EE, then you can provide the Hibernate Physical Naming Strategy via the persistence.xml property file:

<property 
    name="hibernate.physical_naming_strategy"
    value="io.hypersistence.utils.hibernate.naming.CamelCaseToSnakeCaseNamingStrategy"
/>

Now, when generating the database schema using the hbm2ll, Hibernate will execute the following DDL statements:

CREATE SEQUENCE hibernate_sequence
START WITH 1 INCREMENT BY 1

CREATE TABLE book_author (
    id          BIGINT NOT NULL,
    first_name  VARCHAR(255),
    last_name   VARCHAR(255),
    PRIMARY KEY (id)
)

CREATE TABLE paper_back_book (
    id              BIGINT NOT NULL,
    isbn            VARCHAR(255),
    published_on    DATE, 
    title           VARCHAR(255),
    published_by_id BIGINT, 
    PRIMARY KEY (id)
)

Much better, right?

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.

Seize the deal! 40% discount. Seize the deal! 40% discount.

Conclusion

While it’s widely known that you can customize the JPA to DB identifier mapping using the name attribute of JPA @Column annotation, using a Hibernate strategy to apply a given naming convention automatically to tens or hundreds of entities is way more convenient.

And, you don’t even have to write the naming strategy yourself since you can get it via the Hypersistence Utils open-source project.

Transactions and Concurrency Control eBook

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.