HibernateException:缺少列:名称错误

HibernateException: Missing column: Wrong name

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

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

错误:

Caused by: org.hibernate.HibernateException: Missing column: is_negative in datasource.item

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

这取决于您的配置,因为您将 spring.jpa.hibernate.naming.physical-strategy 设置为 PhysicalNamingStrategyStandardImpl,这将对名称使用下划线。

如果你检查 Configure Hibernate Naming Strategy section of Spring Docs,你可以看到:

Hibernate uses two different naming strategies to map names from the object model to the corresponding database names. The fully qualified class name of the physical and the implicit strategy implementations can be configured by setting the spring.jpa.hibernate.naming.physical-strategy and spring.jpa.hibernate.naming.implicit-strategy properties, respectively. Alternatively, if ImplicitNamingStrategy or PhysicalNamingStrategy beans are available in the application context, Hibernate will be automatically configured to use them.

By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy. This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel casing is replaced by underscores as well. By default, all table names are generated in lower case, but it is possible to override that flag if your schema requires it.

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

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

您需要 spring.jpa.hibernate.naming.physical-策略和 spring.jpa.hibernate.naming.implicit-策略

添加关注

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。

希望对您有所帮助。

请看下面我的分析:

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

如果您不想提供注释并希望手动处理 table 名称和列名称,您应该扩展 class org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 并覆盖所需的方法。如果您仍对此处的某些情况使用注解,请记住重写的方法将应用于这些注解中写入的名称。 spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy