Last updated: 2023-10-26

  1. Learn
  2. Spring Data
  3. Naming Best Practices

Best Practices for mapping entities to database tables

Spring Boot provides a powerful integration with Hibernate / JPA using Spring Data. How can we configure the names of the entities and tables in the best possible way?


First some background: suppose we have an entity This directly corresponds to the logical name as it is known for Hibernate and used for example in HQL queries.

A very simple entity

In the next step the logical name is converted to the physical name - without further configuration this corresponds 1:1 to the logical name. In the last "finally final" step, the naming strategy is used to convert the physical name to the actual table name. This is done by default by the CamelCaseToUnderscoresNamingStrategy, so our entity is associated with a table "car_part".

Best practices for entity naming

The following best practices have proven to be useful in order to leverage the wide range of options available.

  • For a new database / application, the Spring Boot default should be used if possible. With this, the logical name corresponds to the class name and the table name is derived from it in lowercase with underscores.
  • The class name and thus the logical name should be defined in the singular, since we work in the code with individual objects, which may be combined in a list. To stay in the example: we instantiate a CarPart and not a CarParts.
  • The logical name should never be overwritten and always correspond exactly to the class name - i.e. no @Entity(name = "CarComponent"). This avoids confusion in HQL queries and log files.
  • For individual deviations of the table name, the physical name can be overwritten with @Table(name = "CarParts").
  • For general deviations of all table names, a custom naming strategy can be defined. This can be used, for example, to change all table and field names to uppercase or camel case, or to add a prefix to each table name. An example of this can be found here.

Only very rarely there are mixed notations in legacy databases where a central name strategy is not adequate. Then the final name must be specified in the @Table annotation and passed on unchanged by the naming strategy.

Keep track of your configuration and get a clean ORM layer by following these guidelines.

Create database schema with Bootify

Using the Bootify Builder, any custom database schema for your next Spring Boot application can be created - directly in the browser, no registration required. All best practices are implemented. In the Profesionnal plan, an option for naming strategies and many more features are available.

See Pricing
or read quickstart