Hibernate命名策略

Ale*_*lex 7 spring hibernate hibernate-annotations spring-boot

我正在使用Spring(Boot)构建REST Web服务,并尝试使用hibernate作为orm mapper而不使用任何xml配置.

我基本上可以使用它,但我遇到了配置问题.我实例LocalContainerEntityManagerFactoryBean作为@Bean一个@Configuration文件.

hibernate.ejb.naming_strategy在下面的例子中设置 - >这似乎适用于创建表,如果它们不存在(列名是像我的@Entity类中的camelCase)但是当执行查询时,hibernate"忘记"关于这个命名配置和尝试使用另一种命名策略与under_score_attributes - >显然这些查询失败.我需要设置其他任何属性吗?

或者另一种配置属性的方法,最好不要添加cfg.xml或者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();
Run Code Online (Sandbox Code Playgroud)

Dav*_*yer 22

HibernateJpaAutoConfiguration允许通过本地外部配置设置命名策略(以及所有其他JPA属性).例如,在application.properties:

spring.jpa.hibernate.ddl_auto: create
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
Run Code Online (Sandbox Code Playgroud)