spring Hibernate 命名策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19146246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate naming strategy
提问by Alex
I am building a REST webservice with Spring (Boot) and am trying to use hibernate as orm mapper without any xml configuration.
我正在使用 Spring (Boot) 构建一个 REST web 服务,并尝试在没有任何 xml 配置的情况下使用 hibernate 作为 orm 映射器。
I basically got it to work, but I am stuck with a configuration issue.
I instantiate LocalContainerEntityManagerFactoryBeanas @Beanin a @Configuration file.
我基本上让它工作了,但我遇到了配置问题。我实例LocalContainerEntityManagerFactoryBean作为@Bean一个@Configuration文件。
I set hibernate.ejb.naming_strategyas 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?
我hibernate.ejb.naming_strategy在下面的例子中设置 -> 如果表不存在,这似乎可以用于创建表(列名是像我的@Entity 类中的驼峰命名法)但是当执行查询时,休眠“忘记”这个命名配置和尝试使用另一种命名策略与 under_score_attributes --> 显然这些查询失败。我需要设置任何其他属性吗?
Or another way of configuring the properties preferably withoutadding a cfg.xmlor persistence.xml?
或者另一种配置属性的方式最好不添加 acfg.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();
回答by Dave Syer
HibernateJpaAutoConfigurationallows the naming strategy (and all other JPA properties) to be set via local external configuration. For example, in application.properties:
HibernateJpaAutoConfiguration允许通过本地外部配置设置命名策略(和所有其他 JPA 属性)。例如,在application.properties:
spring.jpa.hibernate.ddl_auto: create
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
回答by Jeff Evans
Have you tried setting this particular property using programmatic properties? Or a hibernate.propertiesfile in the package root? Or a JVM system property? All are described here.
您是否尝试过使用编程属性设置此特定属性?还是hibernate.properties包根目录中的文件?还是 JVM 系统属性?所有内容都在此处进行了描述。
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.
根据我的经验,如果您坚持不使用任何 XML(这也是我的偏好),有时很难诊断 Hibernate 问题。如果没有其他工作,您可能至少会被迫定义一个配置文件。
回答by Alex
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.
必须排除 JpaAutoConfiguration。我仍然认为这可能是一个错误,因为通常当我使用我自己的 @Configuration 时它应该自动“退后”。

