Customizing Physical Table and Column Names in Hibernate

When working with a databsae that enforces consistent naming conventions—such as prefixing all table and columns with ycx_—manually annotating every entity and field using @Table and @Column becomes impractical for large schemas. A more scalable solution is to implement Hibernate’s PhysicalNamingStrategy interface to apply naming rules globally.

The following implementation prepends ycx_ to both table and column names:

package cn.ycx.study.hibernate.strategy;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {

    @Override
    public Identifier toPhysicalTableName(Identifier logicalName, JdbcEnvironment context) {
        return new Identifier("ycx_" + logicalName.getText(), logicalName.isQuoted());
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier logicalName, JdbcEnvironment context) {
        return new Identifier("ycx_" + logicalName.getText(), logicalName.isQuoted());
    }

    @Override
    public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnv) {
        return name;
    }

    @Override
    public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnv) {
        return name;
    }

    @Override
    public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnv) {
        return name;
    }
}

To activate this strategy, configure it in hibernate.cfg.xml:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.auto_quote_keyword">true</property>
        <property name="hibernate.physical_naming_strategy">
            cn.ycx.study.hibernate.strategy.CustomPhysicalNamingStrategy
        </property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping class="cn.ycx.study.hibernate.entity.User"/>
    </session-factory>
</hibernate-configuration>

With this setup, Hibernate generates DDL sttaements that respect the custom naming convention:

create table `ycx_User` (
   ycx_id bigint not null,
   ycx_firstname varchar(255),
   ycx_lastname varchar(255),
   ycx_username varchar(255),
   primary key (ycx_id)
) engine=InnoDB

create table ycx_id_generator (
   next_val bigint
)

Tags: hibernate naming-strategy database java ORM

Posted on Fri, 17 Jul 2026 16:31:15 +0000 by Ellen67