2023-11-27 02:36:21
Hibernate报错ORA-00904: 无效标识符"PAY_TYPE_CODE"的解决方法是检查数据库字段命名规则,并通过调整Hibernate映射配置确保字段名与数据库一致。以下是具体分析与解决方案:
问题根源根据数据库字段定义方式,选择以下方法之一:
方法1:使用@Column注解显式指定字段名(推荐)在Hibernate实体类中,为对应属性添加@Column注解,并指定数据库字段名(需与数据库完全一致,包括大小写和引号)。
示例代码:
@Entitypublic class Payment { @Column(name = ""pay_type_code"") // 数据库字段名为双引号包裹的"pay_type_code" private String payTypeCode;}注意:若数据库字段名为大写(如PAY_TYPE_CODE),则注解中无需引号:
@Column(name = "PAY_TYPE_CODE")private String payTypeCode;Spring Boot项目:在application.properties或application.yml中配置物理命名策略为PhysicalNamingStrategyStandardImpl,强制Hibernate使用原始字段名(不转换大小写)。
# application.propertiesspring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl# application.ymlspring: jpa: hibernate: naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl非Spring Boot项目:在Hibernate配置类中设置:
Properties properties = new Properties();properties.put("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");// 其他配置...登录数据库,检查字段定义:
SELECT column_name FROM user_tab_columns WHERE table_name = 'YOUR_TABLE_NAME';若字段名被双引号包裹(如"pay_type_code"),可考虑修改为无引号的大写形式(如PAY_TYPE_CODE),并同步更新代码中的映射。
通过以上方法,可有效解决Hibernate因字段名不匹配导致的ORA-00904错误。