This article discusses about different naming strategy provided by hibernate along with the shift of naming strategy from hibernate.ejb.naming_strategy in hibernate 4 to hibernate.implicit_naming_strategy and hibernate.physical_naming_strategy in hibernate 5.And at the end, we wil be take a look into implementing a custom naming strategy in hibernate and configuration to run with spring boot application.
Hibernate uses these strategy to map the java entity and attribute name with corresponding relational database and columns name. In hibernate 4, the naming strategy used was hibernate.ejb.naming_strategy. This strategy uses EJB3NamingStrategy , ImprovedNamingStrategy, DefaultComponentSafeNamingStrategy and DefaultNamingStrategy to map the names. EJB3NamingStrategy is the default naming strategy used and it provides camelcase field and table names.While naming foreign key column it uses undescore(_) as a seperation. For example if you have a table with name and table1 with column names as id and name, then in table2 the foreign key column will be created as table1_id and this EJB3NamingStrategy implements NamingStrategy interface.
After the release of Hibernate 5, hibernate.ejb.naming_strategy is no longer avilable as NamingStrategy contract was often not flexible enough to properly apply a given naming "rule". Instead two new strategy was introduced to provide deep customization of the naming strategy and they are ImplicitNamingStrategy and PhysicalNamingStrategy. To use this strategy there are two keys to be used implicit_naming_strategy and physical_naming_strategy.Hibernate 5 provides only one implementation of PhysicalNamingStrategy - PhysicalNamingStrategyStandardImpl but several implementations of ImplicitNamingStrategy.
ImplicitNamingStrategy is used when you don't provide the database and column name explicitly in the entity definition where as PhysicalNamingStrategy can be used to explicitly define mapping rules of the entity and attribute name with the database and columns name.
ImplicitNamingStrategy is used when an entity does not explicitly name the database table that it maps to or when a particular attribute does not explicitly name the database column that it maps to.We can specify which ImplicitNamingStrategy to use using the hibernate.implicit_naming_strategy configuration setting which accepts default, jpa, legacy-hbm, legacy-jpa and component-path.
The idea of a PhysicalNamingStrategy is to define custom naming rules without having to hard-code them into the mapping via explicit names.Following is an implementation of PhysicalNamingStrategy to define custom table name and column name.
To make use of this custom strategy in hibernate, following is the configuration.
As we discussed, hibernate provides two different naming strategy whereas Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy where all dots are replaced by underscores and camel casing is replaced by underscores and all table names are generated in lower case.For example, a USERDETAILS entity is mapped to the user_details table.
If you want to use your own custom naming strategy as implemented above, you can make following configuration in application.properties.
In this article we learned about different naming strategy provided by spring boot and hibernate and ways to implement our own custom naming strategy. If you have anything that you want to add or share then please share it below in the comment section.
2. Spring Data Jpa Example
5. Spring Boot Mvc App With Jsp
9. Hibernate Criteria Tutorial
Contact us: firstname.lastname@example.org