Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate naming strategy

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();
like image 312
Alex Avatar asked Oct 02 '13 20:10

Alex


1 Answers

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
like image 52
Dave Syer Avatar answered Oct 21 '22 05:10

Dave Syer