Hibernate won't let me use entity class name for table name

I have an object called LocationType ( BaseEntity - @MappedSuperclass ):

 @Entity public class LocationType extends BaseEntity 

The table name generated for this object is location_type . I understand that the default naming strategy works as follows.

I do not understand why I cannot force Hibernate to use the literal name LocationType . No matter what I do:

 @Entity(name = "LocationType") public class LocationType 

or

 @Entity @Table(name = "LocationType") public class LocationType 

or

 @Entity(name = "LocationType") @Table(name = "LocationType") public class LocationType 

the table name always ends with location_type . Hibernate just knows better!

If I use any other name

 @Entity(name = "wtf") 

then the table name will also be wtf .

Is this documented behavior? Looks like a mistake.

A similar question: Hibernate ignores @Table (name = "...") for extended classes - created table names are all lowercase (this applies to inheritance mapping, though).

+5
source share
2 answers

It seemed strange to me that Hibernate does not obey what is indicated in the annotation @Table(name="...") , so I dug up this error report from 9 years old:

NamingStrategy should not be used if table or column name is specified

The mistake was rejected, God and Gavin King know why (or maybe it's just God now). This is contrary to JPA 1/2 specifications. The final answer this way: this is how Hibernate works out of the box; if you keep your annotation names expensive (or respecting the JPA), you must implement your own naming strategy to fix this quirk.

+1
source

See here in the docs.

ImprovedNamingStrategy

Implementation of NamingStrategy .

This is the behavior of org.hibernate.cfg.ImprovedNamingStrategy , which converts blended names to the inline underscore name. http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/cfg/ImprovedNamingStrategy.html . Therefore, if you explicitly use the name "EventLog", it is converted to "event_log".

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

If you want to use the ImprovedNamingStrategy tables for all but those that explicitly indicate a name, you can use the subclass below. The columnName and tableName methods are those that are called when the name is explicitly specified; this subclass leaves the given names unchanged.

 public class RespectfulImprovedNamingStrategy extends ImprovedNamingStrategy { @Override public String columnName(String columnName) { return columnName; } @Override public String tableName(String tableName) { return tableName; } @Override public String classToTableName(String className) { return addUnderscores( StringHelper.unqualify(className) ); } } 

sitelinks for reference.

link1

link2

+2
source

Source: https://habr.com/ru/post/1201224/


All Articles