Hibernate 5 Naming Strategy Configuration

1. Overview

Hibernate 5 provides two different naming strategies for use with Hibernate entities: an Implicit Naming Strategy and a Physical Naming Strategy.

In this tutorial, we’ll see how to configure those naming strategies to map entities to customized table and column names.

For readers who are new to Hibernate, make sure to check out our introduction article here.

2. Dependencies

We’ll use the basic Hibernate Core dependency for this tutorial:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.6.Final</version>
</dependency>

3. Implicit Naming Strategy

Hibernate uses a logical name to map an entity or attribute name to a table or column name. This name can be customized in two ways: it can be derived automatically by using an ImplicitNamingStrategy or it can be defined explicitly by using annotations.

The ImplicitNamingStrategy governs how Hibernate derives a logical name from our Java class and property names. We can select from four built-in strategies, or we can create our own.

For this example, we’ll use the default strategy, ImplicitNamingStrategyJpaCompliantImpl. Using this strategy, the logical names will be the same as our Java class and property names.

If we want to deviate from this strategy for a specific entity, we can use annotations to make those customizations. We can use the @Table annotation to customize the name of an @Entity. For a property, we can use the @Column annotation:

@Entity
@Table(name = "Customers")
public class Customer {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    @Column(name = "email")
    private String emailAddress;

    // getters and setters

}

Using this configuration, the logical names for the Customer entity and its properties would be:

Customer -> Customers
firstName -> firstName
lastName -> lastName
emailAddress -> email

4. Physical Naming Strategy

Now that we configured our logical names, let’s have a look at our physical names.

Hibernate uses the Physical Naming Strategy to map our logical names to a SQL table and its columns.

By default, the physical name will be the same as the logical name that we specified in the previous section. If we want to customize the physical names, we can create a custom PhysicalNamingStrategy class.

For example, we may want to use camel case names in our Java code, but we want to use underscore separated names for our actual table and column names in the database.

Now, we could use a combination of annotations and a custom ImplicitNamingStrategy to map these names correctly, but Hibernate 5 provides the PhysicalNamingStrategy as a way to simplify this process. It takes our logical names from the previous section and allows us to customize them all in one place.

Let’s see how this is done.

First, we’ll create a strategy that converts our camel case names to use our more standard SQL format:

public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {

    @Override
    public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return convertToSnakeCase(identifier);
    }

    @Override
    public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return convertToSnakeCase(identifier);
    }

    @Override
    public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return convertToSnakeCase(identifier);
    }

    @Override
    public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return convertToSnakeCase(identifier);
    }

    @Override
    public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
        return convertToSnakeCase(identifier);
    }

    private Identifier convertToSnakeCase(final Identifier identifier) {
        final String regex = "([a-z])([A-Z])";
        final String replacement = "$1_$2";
        final String newName = identifier.getText()
          .replaceAll(regex, replacement)
          .toLowerCase();
        return Identifier.toIdentifier(newName);
    }
}

Finally, we can tell Hibernate to use our new strategy:

hibernate.physical_naming_strategy=com.baeldung.hibernate.namingstrategy.CustomPhysicalNamingStrategy

Using our new strategy against the Customer entity, the physical names would be:

Customer -> customers
firstName -> first_name
lastName -> last_name
emailAddress -> email

5. Conclusion

In this quick article, we’ve learned the relationship between the Implicit and Physical Naming Strategies.

We have also seen how to customize the implicit and physical names of an entity and its properties.

You can view the source code for this tutorial over on Github.

Leave a Reply

Your email address will not be published.