Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Naming Strategy in Hibernate - NamingStrategy

DZone's Guide to

Naming Strategy in Hibernate - NamingStrategy

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}