AspectJ/AspectWerkz. JDO 2.0 Ballot Results. The good and the bad. Acegi Security Announces Version 0.7
Jan 19

Hibernate ImprovedNamingStrategy

Tech Add comments

By default, a Hibernate class named FooBar will become a table named FooBar.

A lot of the time you don’t want this behaviour as:

  • You are mapping to existing schema which has different table names (e.g. foo_bar)
  • You have corporate standards
  • You just don’t like camel case for DB items

I have seen a lot of projects where the table name is manually added in:

@hibernate.class table="foo_bar"

... column="last_updated"

That is fine of course, but if you want to do it across the board you can nicely use the net.sf.hibernate.cfg.ImprovedNamingStrategy.

You need to set this up in Hibernates configuration. It isn’t a property (so you don’t just put it in the file). Instead you need to have setNamingStrategy(…) called.

If you are using Spring to manage hibernate you can just do the following:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="dataSource"><ref local="dataSource" /></property>

<!-- Must references all OR mapping files. -->
<property name="mappingDirectoryLocations">

<!-- Set the various Hibernate properties. E.g. type of database; changing this one property will port this to Oracle, MS SQL etc. -->
<property name="hibernateProperties">
<prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
<prop key="">${}</prop>
<prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop>

<property name="namingStrategy"><ref bean="namingStrategy"/></property>

<bean id="namingStrategy" class="net.sf.hibernate.cfg.ImprovedNamingStrategy"/>

This is one naming strategy, which handles a simple CamelCase to camel_case conversion.

You can even write your own to do more complicated things. I saw a mapper like this on another ORM product which was even smart enough to do mappings such as making the tables plural. It even handled the case:

Person class to people table

Comments are closed.