Learnitweb

Naming strategies in Hibernate

1. Introduction

Hibernate as an ORM mapping tool maps objects in domain model to the relational model. To do this mapping, Hibernate has to identify which Java object to map with which relational model. ImplicitNamingStrategy and PhysicalNamingStrategy are used to identify how the mapping of names should be done. The mapping of names is done in two stages:

  1. In the first stage, a logical name is determined from the domain model mapping. You can explicitly specify the logical name using annotations like @Table, @Column. Or it can be implicitly determined by Hibernate using ImplicitNamingStrategy implementation.
  2. In the second stage, the logical name is resolved to a physical name which is defined by the PhysicalNamingStrategy implementation.

Naming strategies definition are very helpful to map domain model to relation model. Sometimes, the names in domain model and relational model are different and if no strategy is defined then you may end up providing the mapping details repeatedly.

2. ImplicitNamingStrategy

You can explicitly provide the name of the table or the column using @Table and @Column. When an entity does not explicitly provides the name of the table or column to which it maps, Hibernate implicitly determines the name of the table or column. The Interface used to define this contract is org.hibernate.boot.model.naming.ImplicitNamingStrategy. There are two ways to do the configuration:

  1. Using the hibernate.implicit_naming_strategy configuration setting. This configuration setting accepts reference or a fully qualified name of a Class that implements org.hibernate.boot.model.naming.ImplicitNamingStrategy. Hibernate has out-of-the-box implementations which can also be used to do configuration.
  2. Using org.hibernate.boot.MetadataBuilder#applyImplicitNamingStrategy.

3. PhysicalNamingStrategy

Many organizations have their own rules for naming of database objects like tables, columns, sequences etc. These rules can be hard coded when we define mapping. The other way is to use PhysicalNamingStrategy. For example, you may want to abbreviate number with num, phone with ph and add certain prefixes like TAB_ to the table names.

In our example, we’ll demonstrate using PhysicalNamingStrategy by adding prefix “TAB_” for the table name. So if the entity is Employee, the physical table name will be TAB_Employee. So configuring PhysicalNamingStrategy will help you mapping TAB_ without hard coding in your code.

In hibernate.cfg.xml, we’ll configure hibernate.physical_naming_strategy property.

<property name="hibernate.physical_naming_strategy">com.learnitweb.util.CustomPhysicalNamingStrategy</property>

Here, CustomPhysicalNamingStrategy is a class which extends PhysicalNamingStrategyStandardImpl. We’ll override method toPhysicalTableName to provide logic for deciding physical name for the table.

package com.learnitweb.util;

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

public class CustomPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {

	@Override
	public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		// add prefix TAB_ for the table name
		return jdbcEnvironment.getIdentifierHelper().toIdentifier("TAB_" + name.getText());
	}
}