DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Naming Strategy in Hibernate - NamingStrategy

Naming Strategy in Hibernate - NamingStrategy

Sudhir Mongia user avatar by
Sudhir Mongia
·
Aug. 27, 10 · Java Zone · Interview
Like (0)
Save
Tweet
42.98K Views

Join the DZone community and get the full member experience.

Join For Free

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

Hibernate Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SQL Database Schema: Beginner’s Guide (With Examples)
  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • How Low Code Demands More Creativity From Developers
  • SQL vs. NoSQL: Pros and Cons

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo