Hibernate命名策略

7
我正在使用Spring(Boot)构建REST webservice,并尝试在没有任何xml配置的情况下使用hibernate作为ORM映射器。我基本上让它工作了,但我卡在了一个配置问题上。我在@Configuration文件中将LocalContainerEntityManagerFactoryBean实例化为@Bean。我像以下示例一样设置hibernate.ejb.naming_strategy -> 如果表不存在,则这似乎适用于创建表(列名与@Entity类中的camelCase相同),但是当执行查询时,hibernate会“忘记”此命名配置并尝试使用另一种下划线属性的命名策略->显然这些查询失败。是否还有其他需要设置的属性?或者更喜欢不添加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();
3个回答

22

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

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

但是我在哪里找到这些属性的文档呢?! - Alex
回答我的评论:https://github.com/spring-projects/spring-boot/blob/master/docs/application.yml 这非常有用...适用于所有自动配置属性。 - Alex
当前查找属性的位置是http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties。 - TalkLittle
具体请参考http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties。 - user2964500

0

你尝试过使用编程属性来设置这个特定的属性吗?或者在包根目录下使用hibernate.properties文件?或者使用JVM系统属性?所有这些都在这里有描述。

根据我的经验,如果你坚持不使用任何XML(这也是我的首选),有时候很难诊断Hibernate的问题。如果其他方法都不起作用,你可能被迫至少定义一个配置文件。


-2

我现在有解决方案了。

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

必须排除JpaAutoConfiguration。但我认为这可能是一个bug,因为通常当我使用自己的@Configuration时,它应该自动“退后”。


这样做有点违背使用Spring Boot的初衷。请看下面我的回答。 - Dave Syer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接