Improvednamingstrategy No Longer Working in Hibernate 5

Hibernate ImprovedNamingStrategy doesn't work

Just to put it explicitly(as i had to spend some time figuring it out from Maciej's answer), this works for me on Hibernate 5.x:

properties.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");

Hibernate 5.1.x naming Strategy (backward compatible with Hibernate 4.x)

Firstly, you don't need
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

because of it does nothing and is used by Hibernate as default.

Hibernate 5 doesn't have a strategy that you want. All strategies are JPA compliant (generate names like AppUser). So you need to implement your own.

For an example a physical naming strategy

public class UnderscorePhysicalStartegy extends PhysicalNamingStrategyStandardImpl {

@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return context.getIdentifierHelper()
.toIdentifier(NamingStrategyUtils.classToName(name.getText()));
}

}

It uses NamingStrategyUtils.

Keep in mind, if you specify an explicit name

@Entity
@Table(name = "AppUser")
public class AppUser {

}

you will have anyway a table name app_user. If you don't want such behavior use an implicit naming strategy.

I did some research work on naming strategies. You can refer Hibernate5NamingStrategy, it generates table and column names with underscores like you need and constraint names (unique, foreign key) as well.

This class is used to generate names: HibernateNamingStrategy.

How to use Hibernate5NamingStrategy

The naming strategy can be configured using StrategyOptions.

For example, to use strategy without the prefixes (like f_):

StrategyOptions options = StrategyOptions.builder().withoutPrefixes().build();
Hibernate5NamingStrategy strategy = new Hibernate5NamingStrategy(options);

Other examples: Hibernate 5 Implicit Naming Strategy

Except that, ImprovedNamingStrategy for Hibernate 5 can be used to simulate the behaviour of Hibernate 4 ImprovedNamingStrategy.

Implementing a NamingStrategy in Hibernate 5 (make autogenerated column names UPPERCASE)

Hibernate 5 uses two new interfaces for name strategies PhysicalNamingStrategy and ImplicitNamingStrategy. You need just implement PhysicalNamingStrategy. It is called by Hibernate after all column names are created for model. So you can just make it uppercase. Hibernate uses by default PhysicalNamingStrategyStandardImpl, that do nothing. You can just extend it

public class UpperCaseNamingStrategy extends PhysicalNamingStrategyStandardImpl {

@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return context.getIdentifierHelper().toIdentifier(
StringUtils.upperCase(name.getText(), Locale.ENGLISH));
}

}

You can build session factory with UpperCaseNamingStrategy by this way

    Configuration configuration = new Configuration();
configuration.setPhysicalNamingStrategy(new UpperCaseNamingStrategy());
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();

I am working on a more complex name strategy now. You can refer Hibernate5NamingStrategy if you are interested.

change Hibernate 5 naming strategy + spring boot + annotation

With Hibernate 5, you should be using different properties:

spring.jpa.hibernate.naming.implicit-strategy= # Hibernate 5 implicit naming strategy fully qualified name.
spring.jpa.hibernate.naming.physical-strategy= # Hibernate 5 physical naming strategy fully qualified name.

As you can see here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties

hibernate ImprovedNamingStrategy overrides Table name in entity

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

Hibernate 4 - 5 migration: NamingStrategy changes, Tables not found

Solved this by using the newest SQL Server JDBC Driver. A had an old one from 2012. Now i downloaded and used the newest JDBC 4.2 from https://www.microsoft.com/en-us/download/details.aspx?id=11774 (sqljdbc_4.2.6420.100_enu.exe -> sqljdbc42.jar) and it started working. I could even revert the default schema changes for the SQL user.



Related Topics



Leave a reply



Submit