如果连接关系在嵌入式上,则如何使用标准API加入两个表



我有以下类:

@Entity 
public class EventOrderLine {
  @EmbeddedId   private EventOrderLineId id;
}
@Embeddable
public class EventOrderLineId implements Serializable {
  @ManyToOne
  @JoinColumn(name = "eventid")
  @JsonIgnore
  private Event event;
  @ManyToOne
  @JoinColumn(name = "orderlineid")
  @JsonIgnore
  private OrderLine orderLine;
}

@Entity   
public class OrderLine {
  @OneToMany
  @JoinColumn(name = "orderlineid")
  @JsonIgnore
  private List<EventOrderLine> eventOrderLines = new ArrayList<>()
}

基本上,我正在尝试通过标准API加入两个表,但是有问题,因为这是我要做的:

Root eventOrderLine = criteriaQuery.from(EventOrderLine.class);
Join orderLine = eventOrderLine.join("orderLine");

当然,这给了我这个问题,因为映射不直接在实体本身上:

Unable to locate Attribute  with the the given name [orderLine] on this ManagedType [com.EventOrderLine]

我一直在尝试调整连接以钻入嵌入式启动,但不确定我是否需要进一步修改我的实体的映射方式。我觉得这可能是简单的我缺少的东西,但是很难找到这个特定的问题。

event字段是EventOrderLineId的成员,而不是EventOrderLine。在您的标准查询中,您首先需要导航到id。发现Root.path("id")返回Path的实例,该实例不允许进一步连接。

诀窍是使用"假"与id字段加入:eventOrderLine.join("id").join("event")

eventOrderLine.get("id").get("event")也可能工作,但它不允许您指定加入类型。

首先尝试获取EventOrderLine实体的属性id,然后加入。所以,这将是 -

Root eventOrderLine = criteriaQuery.from(EventOrderLine.class);
Join orderLine = eventOrderLine.get("id").join("orderLine")

最新更新