Hibernate naming strategy

Alex picture Alex · Oct 2, 2013 · Viewed 26k times · Source

I am building a REST webservice with Spring (Boot) and am trying to use hibernate as orm mapper without any xml configuration.

I basically got it to work, but I am stuck with a configuration issue. I instantiate LocalContainerEntityManagerFactoryBean as @Bean in a @Configuration file.

I set hibernate.ejb.naming_strategy as in the following example -> this seems to work for creating the tables if they do not exist (column names are camelCase like in my @Entity classes) but when a query is executed, hibernate "forgets" about this naming configuration and tries to use another kind of naming strategy with under_score_attributes --> obviously those queries fail. Is there any other property I need to set?

Or another way of configuring the properties preferably without adding a cfg.xml or persistence.xml?

LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();   
Properties props = new Properties();
props.put("hibernate.hbm2ddl.auto", "create");
props.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.DefaultNamingStrategy");
lef.setJpaProperties(props); 
lef.afterPropertiesSet();

Answer

Dave Syer picture Dave Syer · Nov 4, 2013

HibernateJpaAutoConfiguration allows the naming strategy (and all other JPA properties) to be set via local external configuration. For example, in application.properties:

spring.jpa.hibernate.ddl_auto: create
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy