Hibernate的命名策略 [英] Hibernate naming strategy

查看:108
本文介绍了Hibernate的命名策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我基本上已经开始使用它了,但我坚持配置问题。
在@Configuration文件中实例化 LocalContainerEntityManagerFactoryBean 作为 @Bean



我设置了 hibernate.ejb.naming_strategy ,如下例 - >这似乎适用于创建表(如果它们不存在的话)像在我的@Entity类中那样是camelCase),但是执行查询时,hibernate会忘记这个命名配置,并尝试使用另一种具有under_score_attributes的命名策略 - >显然这些查询会失败。我还需要设置其他任何财产吗?



或者另一种配置属性的方式,最好不要添加 cfg.xml persistence.xml

  LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 
属性props = new Properties();
props.put(hibernate.hbm2ddl.auto,create);
props.put(hibernate.ejb.naming_strategy,org.hibernate.cfg.DefaultNamingStrategy);
lef.setJpaProperties(道具);
lef.afterPropertiesSet();


解决方案

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

  spring.jpa.hibernate .ddl_auto:创建
spring.jpa.hibernate.naming_strategy:org.hibernate.cfg.EJB3NamingStrategy


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();

解决方案

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

这篇关于Hibernate的命名策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆