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

Olingo OData4 JPA Processor使用$expand关联查询时触发"Could not resolve attribute 'unit_key'"错误求助

Olingo OData4 JPA Processor使用$expand关联查询时触发"Could not resolve attribute 'unit_key'"错误求助

大家好,我现在正在尝试结合JPA使用OData,用的是Spring Data JPA + Hibernate(不是EclipseLink),数据库是本地H2内存库。暂时先不贴配置类,我觉得问题应该不在配置上,如果大家觉得配置可能有影响,我再补充。

我有两个实体Department和Unit,都继承自Area基类,现在想通过$expand来查询Department关联的Unit(ManyToOne关系),反过来查也报同样的错误。

先贴一下实体代码:

基类Area

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Area extends BaseEntity {
    private Integer areaId;
    @Column(length = 10000)
    private String comment;
    private Instant changedDate;
}

Department实体

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Department extends Area {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "unit_key", referencedColumnName = "pkey")
    private Area unit;
}

Unit实体

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Unit extends Area {
    private Integer unitId;
    private String address;
    @OneToMany(mappedBy = "unit", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<Area> units = new ArrayList<>();
}

查看OData的$metadata时,Department的定义看起来是正常的:

return (<EntityType Name="Department" BaseType="masterdata.Area">
  <NavigationProperty Name="Unit" Type="masterdata.Area"/>
)

我执行的查询URL是:

masterdata/v1/Departments(<id>)?$expand=Unit

但得到的错误是:

Could not resolve attribute 'unit_key' of '...model.odataExposed.Department'

感觉框架好像把数据库字段unit_key当成了实体类的属性来解析,但实体里明明是unit这个属性啊...

下面是简化后的堆栈信息:

"stacktrace": {
  "exceptions": [
    {
      "class": "org.hibernate.query.sqm.PathElementException",
      "message": "Could not resolve attribute 'unit_key' of '...model.odataExposed.Department'",
      "invocation": {
        "class": "org.hibernate.query.sqm.SqmPathSource",
        "method": "getSubPathSource",
        "line": 95
      }
    }
  ],
  "stacktrace": [
    { "class": "org.hibernate.query.sqm.SqmPathSource", "method": "getSubPathSource", "line": 95 },
    { "class": "org.hibernate.query.sqm.tree.domain.AbstractSqmPath", "method": "get", "line": 196 },
    { "class": "org.hibernate.query.sqm.tree.domain.AbstractSqmPath", "method": "get", "line": 42 },
    { "class": "com.sap.olingo.jpa.processor.core.query.ExpressionUtility", "method": "convertToCriteriaPath", "line": 82 },
    { "class": "com.sap.olingo.jpa.processor.core.query.JPAAbstractQuery", "method": "createSelectClause", "line": 294 },
    { "class": "com.sap.olingo.jpa.processor.core.query.JPAJoinQuery", "method": "execute", "line": 43 },
    { "class": "com.sap.olingo.jpa.processor.core.processor.JPANavigationRequestProcessor", "method": "retrieveData", "line": 93 },
    { "class": "com.sap.olingo.jpa.processor.core.api.JPAODataRequestProcessor", "method": "readEntity", "line": 211 },
    { "class": "org.apache.olingo.server.core.ODataDispatcher", "method": "handleSingleEntityDispatching", "line": 621 },
    { "class": "org.apache.olingo.server.core.ODataHandlerImpl", "method": "process", "line": 89 },
    { "class": "org.apache.olingo.server.core.ODataHttpHandlerImpl", "method": "process", "line": 74 },
    { "class": "org.apache.olingo.server.core.ODataHttpHandlerImpl", "method": "process", "line": 88 },
    { "class": "com.sap.olingo.jpa.processor.core.api.JPAODataRequestHandler", "method": "processInternal", "line": 71 },
    { "class": "com.sap.olingo.jpa.processor.core.api.JPAODataRequestHandler", "method": "process", "line": 55 },
    { "class": "java.lang.reflect.Method", "method": "invoke", "line": 568 },
    { "class": "org.springframework.aop.support.AopUtils", "method": "invokeJoinpointUsingReflection", "line": 354 },
    ...
  ]
}

有没有遇到过类似问题的朋友?麻烦帮忙看看问题出在哪了,谢谢!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:23:00