You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JPA Specification查询报错无法找到[company_name]属性如何解决

错误根因

JPA在Vendor实体类的托管属性中找不到名为company_name的字段:root.get()方法的入参必须是Java实体类的字段名,而非数据库表的列名,出现该报错通常是以下几类配置错误导致:

修复方案

  • 检查VendorSpecs.getVendorById方法的字段拼写:确认该方法内root.get()的入参没有误写为company_name,若存在拼写错误直接改为实体类对应的字段名即可。
  • 修正Spring JPA的命名策略配置:错误的命名转换策略会导致JPA自动将驼峰格式的实体字段名错误映射,在application.yml/application.properties中添加以下配置:
spring:
  jpa:
    hibernate:
      naming:
        physical-naming-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-naming-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
  • 优化Specification查询写法:你当前的getVendorByName方法中criteriaBuilder.or(predicateName)属于冗余写法,单查询条件可以直接返回谓词对象,优化后代码如下:
public static Specification<Vendor> getVendorByName(String vendorName) {
    String lowerName = "%" + vendorName.toLowerCase() + "%";
    return (root, query, criteriaBuilder) -> {
        Expression<String> expName = root.get("companyName").as(String.class);
        return criteriaBuilder.like(criteriaBuilder.lower(expName), lowerName);
    };
}
  • 若上述方案均无效,检查父类Audit的配置:确认是否存在@AttributeOverride注解错误覆盖了companyName字段的映射规则,或者父类中存在重名冲突字段。

内容的提问来源于stack exchange,提问作者adhi mf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:27:03