Over a million developers have joined DZone.
Silver Partner

Naming Strategy in Hibernate - NamingStrategy

· Java Zone

The Java Zone is brought to you in partnership with DCHQ.  Discover how to deploy & monitor multi-tier Java applications on any cloud in seconds.

Recently while exploring I Hibernate I came to know a very nice feature of Hibernate which gives a handle to map naming convention of underlying tables and columns with the Objects and properties. I found it very interesting and easy to use functionality.

Problem Scenario:

Generally while using Hibernate either we keep the tables names corresponding to the POJO names similar for ease of use, or we define the table name in the corresponding mapping hbm file. You may come across the following different scenarios.

  • Sometimes you may need to change the underlying table name or may need to change the naming convention followed in the database schema.
  • You may need to have separate tables for different clients using the same application. A typical scenario is the hosted and multitenant environment.

Code Example
Lets see some a mapping example for class and table names in the mapping document
1. Explicitly specified class name and table name

<class name="HelloMessage" table="message" />

2. Table name is missing

<class name="HelloMessage" />

Hibernates default behavior tries to look for a table with name HelloMessage in database.

3. Table name is missing

<class name="HelloMessage" />

How I can instruct Hibernate to look for TEST_HelloMessage table or Hello_Message Table.

The first two scenarios are very simple. The third item is the scenario where NamingStrategy comes into picture. We can provide our instruction for table name and class name mapping to Hibernate.

How to Use NamingStrategy?

Hibernate provides the Naming Strategy interface to be implemented by the implementation. I am listed here few methods.
1. String classToTableName(String className) – should return the table name for an entity class.
2. String columnName(String columnName) – handle to alter the column name specified in the mapping document.
3. String tableName(String tableName) – handle to alter the column name specified in the mapping document.
4. String propertyToColumnName(String propertyName) – handle to map property name to column name.

Sample Implementation

/**
* Extending from the DefaultNamingStrategy to
avoid implementing All methods of NamingStrategy interface
*/
public class HelloWorldNamingStrategy
extends DefaultNamingStrategy {
//Extending from the DefaultNamingStrategy to
//avoid implementing All methods of NamingStrategy interface
public String classToTableName(String className) {
//need to get the className only
int dotPos = className.lastIndexOf(".");
String classNameWithoutPackageName
= className.substring(dotPos+1,className.length());
return classNameWithoutPackageName + "_Messages";
}
public String propertyToColumnName(String propertyName) {
return propertyName;
}
}

Mapping Document:

<hibernate-mapping>
<class name="hello.HelloWorld">
// table name is not mentioned
<id column="MESSAGE_ID" name="id">
<generator class="increment">
</generator></id>
<property column="MESSAGE_TEXT" name="text">
</property></class>
</hibernate-mapping>

Passing Naming Strategy to Hibernate

Configuration conf = new Configuration().configure();

//Passing my own implementation for strategy interface
conf.setNamingStrategy(new HelloWorldNamingStrategy());

//I can use the improved naming strategy provided by hibernate
//conf.setNamingStrategy(new ImprovedNamingStrategy());
sessionFactory= conf.buildSessionFactory();


I have written a very simple implementation of the NamingStrategy here. If you see the flexibility here, it can go to any extent. Hibernate itself provides two implementations with the names DefaultNamingStrategy and ImprovedNamingStrategy. 

From http://sudhirmongia.blogspot.com/2010/08/naming-strategy-in-hibernate.html

The Java Zone is brought to you in partnership with DCHQ. Learn more about best practices for automating the deployment and management of your Java application on any cloud.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}