HibernateException:缺少列:错误的名称

时间:2018-08-17 12:53:49

标签: java spring hibernate hibernate-mapping naming

在我的Java代码中,我有一个名为isNegative的字段,数据库中存在一个类似的列。但是Hibernate坚持认为名称应为is_negative,即使使用@Column强制使用该名称也是如此。

@Column(name="isNegative")
private boolean isNegative;

错误:

  

由于:org.hibernate.HibernateException:缺少列:   datasource.item中的is_negative

Application.properties:

#JPA
spring.data.jpa.repositories.enabled=false
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.properties.hibernate.event.merge.entity_copy_observer=allow
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

3 个答案:

答案 0 :(得分:3)

这是由于您的配置所致,因为您将spring.jpa.hibernate.naming.physical-strategy设置为PhysicalNamingStrategyStandardImpl,因此名称将使用下划线。

如果您选中Configure Hibernate Naming Strategy section of Spring Docs,则会看到:

  

Hibernate使用两种不同的命名策略将名称从对象模型映射到相应的数据库名称。可以分别通过设置spring.jpa.hibernate.naming.physical-strategyspring.jpa.hibernate.naming.implicit-strategy属性来配置物理和隐式策略实现的标准类名。另外,如果在应用程序上下文中有ImplicitNamingStrategyPhysicalNamingStrategy bean,则Hibernate将自动配置为使用它们。

     

默认情况下,Spring Boot使用以下命令配置物理命名策略:   SpringPhysicalNamingStrategy。该实现提供了相同的   表格结构为Hibernate 4:所有点均由下划线代替   并且骆驼肠衣也由下划线代替。默认情况下,所有   表名以小写形式生成,但是可以   如果您的模式需要,请覆盖该标志。

要解决此问题,您需要删除此属性并改用默认命名策略:

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

答案 1 :(得分:0)

您将需要spring.jpa.hibernate.naming.physical-strategy和spring.jpa.hibernate.naming.implicit-strategy

添加关注

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

至application.properties可能会有所帮助。此解决方案将从休眠5开始工作。

希望有帮助。

答案 2 :(得分:0)

请在下面进行分析:

如果您不希望您的命名策略在列名或类名上添加下划线,那么您需要使用的策略应类似于:spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl。您在注释@Table@Column’s name属性中提供的内容将保持原样。例如。实体中的 firstName 属性将获得列名称为 firstName ,即不变。

如果您不想提供注释,并且想要手动处理表名和列名,则应该扩展类org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl并覆盖所需的方法。如果您在某些情况下仍使用注释,请记住,重写的方法将应用于那些注释中编写的名称。 spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy

相关问题