gpt4 book ai didi

java - hibernate 异常 : Missing column: Wrong name

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:20 25 4
gpt4 key购买 nike

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

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

错误:

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

应用程序属性:

#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

关于java - hibernate 异常 : Missing column: Wrong name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51895895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com