Question

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();
Was it helpful?

Solution

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

OTHER TIPS

Have you tried setting this particular property using programmatic properties? Or a hibernate.properties file in the package root? Or a JVM system property? All are described here.

From my experience, there are sometimes difficult to diagnose Hibernate problems if you insist on not using any XML (which would be my preference as well). If nothing else works, you may be forced to define a configuration file at least.

I got the solution now.

@EnableAutoConfiguration(exclude={HibernateJpaAuto Configuration.class}) 

The JpaAutoConfiguration must be excluded. Still I think this might be a bug, as normally it should automatically "stand back" when I use my own @Configuration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top