Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate ImprovedNamingStrategy overrides Table name in entity

Tags:

hibernate

I am using org.hibernate.cfg.ImprovedNamingStrategy, But for a table I have specified the table name explicitly

@Table(name="EventLog",schema = "eventlogs")

But hibernate seems to be looking for event_log. Shouldn't explicit naming override the one provided by ImprovedNamingStrategy

like image 381
user373201 Avatar asked Feb 19 '11 11:02

user373201


2 Answers

If you would like to use the ImprovedNamingStrategy for all tables except those which specify a name explicitly you can use the subclass below. The columnName and tableName methods are the ones called when a name is explicitly specified, this subclass leaves the specified names unmolested.

I think it's odd that this is not the default behaviour.

public class RespectfulImprovedNamingStrategy extends ImprovedNamingStrategy
{
    @Override
    public String columnName(String columnName)
    {
        return columnName;
    }

    @Override
    public String tableName(String tableName)
    {
        return tableName;
    }
}
like image 71
NickTee Avatar answered Nov 15 '22 19:11

NickTee


It is the behavior of the org.hibernate.cfg.ImprovedNamingStrategy , which will convert the mixed case names to the embedded underscores name . http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/cfg/ImprovedNamingStrategy.html . So if you explicitly use the name "EventLog" , it will convert to the "event_log" .

If you simply want to use the name explicitly specified in the @Table , you should use the org.hibernate.cfg.DefaultNamingStrategy . By default it is used when you instantiate your org.hibernate.cfg.Configuration object

like image 6
Ken Chan Avatar answered Nov 15 '22 21:11

Ken Chan