'change Hibernate 5 naming strategy + spring boot + annotation

My spring boot project is using JPA hibernate annotation in entity classes. I have my own generic repository and I'm NOT getting the Hibernate SessionFactory from JPA's entityManagerFactory. The problem occurs when new tables and columns are created. Camel columns are created with an underscore on the database. I changed the naming strategy to org.hibernate.cfg.EJB3NamingStrategy in application.yml but nothing fixed.

application.yml:

 jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect 
    show-sql: true 
    hibernate:
        ddl-auto: update 
        naming-strategy: org.hibernate.cfg.EJB3NamingStrategy

Getting hibernate session in My own Generic repository (not using EntityManager):

@Autowired
public SessionFactory   sessionFactory;

public Session getSession() {
    try {
        return sessionFactory.getCurrentSession();
    } catch (HibernateException e) {
        System.out.println(e.getMessage().toString());
    }
    return sessionFactory.openSession();
}

I used to create a custom namingStrategy extending ImplicitNamingStrategyJpaCompliantImpl but nothing happened!

What is wrong?

What I want:

columns created in camel in the database. even tables!



Solution 1:[1]

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

Solution 2:[2]

If you are providing @Table and @Column annotation in your entity classes with names provided with an underscore i.e. user_id i.e. @Column(name="**user_id**"), it will take the column name as user_id; if you give it as userid then it will change to user_id if you use no strategy or implicit strategy (specifically spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl). So, if you want a strategy where the entity attribute name changes to one with underscore and lowercase letters i.e. something from userId to user_id, you should use implicit or no strategy (which actually uses implicit strategy).

If you don't want your naming strategy to add an underscore to the column name or class name, then the strategy that you need to use would look like: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. The things that you provide in annotations @Table and @Column’s name attribute would remain as it is.

If you don't want to provide annotations and want to manually handle the table name and column names, you should extend the class org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl and override the required methods. If you still use annotations for some of the cases here, remember the overridden methods will apply on the names written in those annotations. spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy

Solution 3:[3]

I'm using Spring Data JPA 2.2.1 and the configuration below works for me

spring.jpa.properties.hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

Solution 4:[4]

My project is using the @Embedded annotation with Hibernate ORM core version 5.4.28.Final and the following worked for me.

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl

The proper setting was found in the Hibernate user guide https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#embeddable-multiple-namingstrategy

Solution 5:[5]

Add this to your application.properties file.

#Get rid of SpringPhysicalNamingStrategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Magd Kudama
Solution 2 mate00
Solution 3 David Buck
Solution 4 ColoradoCurtis
Solution 5 Suraj Rao