1. Introduction

Spring Data JPA offers many features for using JPA in an application. Among those features is the standardization of table and column names in both DDL and DML queries.

In this short tutorial, we’re going to see how to configure this default naming convention.

2. Default Naming Convention

First of all, let’s see what Spring’s default naming convention is regarding table and column names.

Let’s imagine we have a Person entity:

@Entity
public class Person {
    @Id
    private Long id;
    private String firstName;
    private String lastName;
}

We have a few names here that have to be mapped to the database. Well, Spring uses lower snake case by default, which means it uses only lowercase letters and separates words with underscores. Therefore, the table creation query for the Person entity would be:

create table person (id bigint not null, first_name varchar(255), last_name varchar(255), primary key (id));

A selection query returning all first names would be:

select first_name from person;

To do that, *Spring implemented its version of Hibernate’s PhysicalNamingStrategy: SpringImplicitNamingStrategy.*

3. RDMS Case-Sensitivity

Before discussing how to create our custom naming convention in detail, let’s briefly discuss how the RDMS manages the identifiers’ cases.

There are two scenarios to consider: the RDMS is case-sensitive, or it is not.

In the first situation, the RDMS will strictly match identifiers that have the same case. Therefore, in our example, the following query would work:

select first_name from person;

While this one will throw an error and not even return a result:

select FIRST_NAME from PERSON;

On the other hand, for an RDMS that is case-insensitive, both queries would have worked.

What would we do to force the RDMS to match identifiers regarding their case as well? We can use quoted identifiers (for example, “person”).

By using quotes around our identifiers, we tell the database it should also match the case when comparing those identifiers with table and column names. So, still using our example, the following query would work:

select "first_name" from "person";

While this one wouldn’t:

select "first_name" from "PERSON";

That’s in theory, though, because each RDMS manages quoted identifiers differently, so mileage varies.

4. Custom Naming Convention

Now, let’s implement our naming convention.

Imagine we can’t use the Spring lower snake case strategy but need to use the upper snake case. Then, we’d need to provide an implementation of PhysicalNamingStrategy.

As we’re going to keep to snake case, the fastest option is to inherit from the CamelCaseToUnderscoresNamingStrategy and turn identifiers to upper case:

public class UpperCaseNamingStrategy extends CamelCaseToUnderscoresNamingStrategy {
    @Override
    protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
        return new Identifier(name.toUpperCase(), quoted);
    }
}

We’re just overriding the getIdentifier() method, which is responsible for converting the identifiers in the superclass to lowercase. Here, we’re using it to convert them to uppercase.

Once we’ve written our implementation, we must register it so that Hibernate knows how to use it. Using Spring, this is done by setting the spring.jpa.hibernate.naming.physical-strategy property in our application.properties:

spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UpperCaseNamingStrategy

Now, our queries are using upper case identifiers:

create table PERSON (ID bigint not null, FIRST_NAME varchar(255), LAST_NAME varchar(255), primary key (ID));

select FIRST_NAME from PERSON;

Let’s say that we want to use quoted identifiers so that the RDMS is forced to match the case. Then, we would have to use true as the quoted argument of the Identifier() constructor**:

@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
    return new Identifier(name.toUpperCase(), true);
}

After what our queries would present quoted identifiers:

create table "PERSON" ("ID" bigint not null, "FIRST_NAME" varchar(255), "LAST_NAME" varchar(255), primary key ("ID"));

select "FIRST_NAME" from "PERSON";

5. Conclusion

In this short article, we talked about the possibilities of implementing a custom naming strategy using Spring Data JPA and how the RDMS will handle our DDL and DML statements regarding its internal configuration.

As usual, the full code for this article can be found over on GitHub.