开发者

hibernate ImprovedNamingStrategy overrides Table name in entity

开发者 https://www.devze.com 2023-02-11 21:09 出处:网络
I am using org.hibernate.cfg.ImprovedNamingStrategy, But for a table I have specified the table name explicitly

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 ImprovedN开发者_如何转开发amingStrategy


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;
    }
}


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


for hibernate 4 with spring

  spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

for hibernate5 with spring

   spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl


You can explicitely advise hibernate to use the old EJB3NamingStrategy which recognises the annotated table names by defining following property (depending using spring V4+ or not):

Spring:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

No spring (only hibernate):

hibernate.ejb.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
0

精彩评论

暂无评论...
验证码 换一张
取 消