首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HibernateException:缺少列:名称错误

HibernateException:缺少列:名称错误
EN

Stack Overflow用户
提问于 2018-08-17 12:53:48
回答 3查看 782关注 0票数 4

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

代码语言:javascript
复制
@Column(name="isNegative")
private boolean isNegative;

错误:

原因: org.hibernate.HibernateException:缺少列: is_negative in datasource.item

Application.properties:

代码语言:javascript
复制
#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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-17 13:15:16

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

如果您检查 section of Spring Docs,您可以看到:

Hibernate使用两种不同的命名策略将名称从对象模型映射到相应的数据库名称。可以通过分别设置spring.jpa.hibernate.naming.physical-strategyspring.jpa.hibernate.naming.implicit-strategy属性来配置物理实现和隐式策略实现的完全限定类名。或者,如果ImplicitNamingStrategyPhysicalNamingStrategy bean在应用程序上下文中可用,则Hibernate将被自动配置为使用它们。 默认情况下,Spring使用SpringPhysicalNamingStrategy配置物理命名策略。此实现提供了与Hibernate 4相同的表结构:--所有点都被下划线替换,camel大小写被下划线以及替换。默认情况下,所有表名都以小写形式生成,但如果架构需要该标志,则可以重写该标志。

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

代码语言:javascript
复制
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
票数 3
EN

Stack Overflow用户

发布于 2018-08-17 14:19:13

您将需要Spring.jpa.Hibernate.naming.物理-策略和Spring.jpa.Hibernate.naming.隐式-策略

添加以下内容

代码语言:javascript
复制
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可能有帮助。此解决方案将从hibernate 5中运行。

希望能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2019-08-27 12:05:30

以下是我的分析:

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51895895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档