Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate which naming strategy is default

When researching how to implement a custom naming strategy (for table names only) I stumbled upon an inconsistency, which I can't resolve. (I'm using hibernate-core 3.6.6.Final on JBoss 6.1.0.Final with PostgreSQL 9.1.9)

There seem to be three builtin implementations for NamingStrategy:

  1. DefaultNamingStrategy
  2. EJB3NamingStrategy
  3. ImprovedNamingStrategy

The default seems to be set to EJB3NamingStrategy in org.hibernate.cfg.Configuration.

However the table names seem to be set according to a strategy, that matches none of the above.

Example:
Class name: package.ClassName
Resulting table name: classname

Strategies 1 and 2 simply call StringHelper.unqualify( className ) which simply removes all package names and dots, so the result should be ClassName.

Strategy 3 removes all package names and dots, then puts an underscore before each camelcased letter and finally converts to lowercase, which should yield class_name.

(Source code of hibernate 4.1.0.Final seems to be unchanged in these classes.)

Could anyone help me clarify this?

like image 718
weaselflink Avatar asked Aug 08 '13 10:08

weaselflink


People also ask

What is hibernate naming physical strategy?

Hibernate uses the Physical Naming Strategy to map our logical names to a SQL table and its columns. By default, the physical name will be the same as the logical name that we specified in the previous section. If we want to customize the physical names, we can create a custom PhysicalNamingStrategy class.

What is the rule of jpa naming convention?

Googling "jpa entity default table name" you'll most probably stumble onto the following result: The JPA default table name is the name of the class (minus the package) with the first letter capitalized. Each attribute of the class will be stored in a column in the table.

What is spring physical naming strategy?

By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy . This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel casing is replaced by underscores as well.

What is strategy in Hibernate?

Hibernate uses a fetching strategy to retrieve associated objects if the application needs to navigate the association. Fetch strategies can be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria query.


1 Answers

Very easy! Because I don't want to spend time to create toy environment with hibernate-core 3.6.6.Final I give you instruction how to check which instance used EXACTLY IN YOUR WORKING APPLICATION.

Install visualvm on your host. It allow to create heap dump for local application. To create heap dump on remove server - run on server:

jmap -dump:format=b,file=heap.dump $PID

Open dump in visualvm and run OQL query:

map(heap.objects('org.hibernate.cfg.Configuration'), "it.namingStrategyDelegator")

That is YOUR NAMING STRATEGY. You may use "Instance" tab to navigate across reference hierarchy to get known what other actual types of hbm or jpa NamingStrategy is used.

Different version of Hibernate have different declared fields and types. If there are no namingStrategyDelegator field try digging to instance:

heap.objects('org.hibernate.cfg.Configuration')

If that class isn't in your version of Hibernate - try read docs and sources and find appropriate class.

With dependency org.hibernate:hibernate-entitymanager:jar:4.3.11.Final I have:

map(heap.objects('org.hibernate.cfg.Configuration'), "it.namingStrategyDelegator")
      == org.hibernate.cfg.naming.LegacyNamingStrategyDelegator

and it uses internally:

LegacyHbmNamingStrategyDelegate
LegacyJpaNamingStrategyDelegate

depending on presents of JPA annotations. In any cases this instances reference to LegacyNamingStrategyDelegator (yea, cyclic references!) which have field namingStrategy with instance of org.hibernate.cfg.EJB3NamingStrategy.

That all! I spend 1 min to find default naming strategy implementation in my wepapp and 5 min to write this post ((

Thanks for watching, happy hacking!

like image 181
gavenkoa Avatar answered Oct 13 '22 06:10

gavenkoa