SQL Zone is brought to you in partnership with:

I am Sudhir. I am a techie with 7 years of Hands on experience on Java and J2EE. Currently I have got exposure to Finance domain and BPM. Sudhir has posted 2 posts at DZone. You can read more from them at their website. View Full User Profile

Naming Strategy in Hibernate - NamingStrategy

08.27.2010
| 16464 views |
  • submit to reddit

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

Published at DZone with permission of its author, Sudhir Mongia.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jens Schauder replied on Sat, 2010/08/28 - 10:21am

NamingStrategies are also usefull for overcomming limitations of the rdbms used. E.g. Oracle which has a very annoying limit for table and column names of 30 characters.

 

Sometime ago I wrote a NamingStrategy to handle the abbrevating of names in order to match that limit:

http://code.google.com/p/hibernate-naming-strategy-for-oracle/downloads/list

Sudhir Mongia replied on Sat, 2010/08/28 - 11:30am in response to:

Yes, thats.. another important use of this interface.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.