我有以下类(class):
@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<>()
}
基本上,我尝试通过 Criteria 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]
我一直在尝试调整连接以钻取嵌入的 ID,但不确定是否需要更进一步并修改实体的映射方式。我觉得这可能是我缺少的一些简单的东西,但很难找到这个具体问题。
最佳答案
event
字段是 EventOrderLineId
的成员,而不是 EventOrderLine
的成员。在条件查询中,您首先需要导航到 id
。问题是 Root.path("id")
返回一个 Path
实例,它不允许进一步连接。
诀窍是对 id
字段使用“假”连接,如下所示:eventOrderLine.join("id").join("event")
eventOrderLine.get("id").get("event")
可能也同样有效,但它不允许您指定连接类型。
关于java - 如果联接关系位于 EmbeddedId 上,如何使用 Criteria API 联接两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49985825/