Hibernate命名策略

2024-01-20

我正在使用 Spring(Boot)构建 REST Web 服务,并尝试使用 hibernate 作为 orm 映射器,而无需任何 xml 配置。

我基本上可以正常工作,但我遇到了配置问题。 我实例化LocalContainerEntityManagerFactoryBean as @Bean在@Configuration 文件中。

I set hibernate.ejb.naming_strategy如下面的示例所示 - >如果表不存在,这似乎适用于创建表(列名是驼峰式,就像我的 @Entity 类中一样),但是当执行查询时,hibernate“忘记”此命名配置并尝试使用 under_score_attributes 的另一种命名策略 --> 显然这些查询会失败。我还需要设置其他属性吗?

或者最好配置属性的另一种方式without添加一个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允许通过本地外部配置设置命名策略(以及所有其他 JPA 属性)。例如,在application.properties:

spring.jpa.hibernate.ddl_auto: create
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hibernate命名策略 的相关文章

随机推荐