JPQL 中的嵌套查询

Nested query in JPQL

我有 2 个实体 -

@Table(name = "MM_MONITORING_CARD")
@Entity(name = "mm_MonitoringCard")
public class MonitoringCard extends StandardEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID")
private Counterparty client;

@Column(name = "START_DATE")
private LocalDate startDate;

@Column(name = "END_DATE")
private LocalDate endDate;

...otherColumn
}

@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
@Table(name = "MM_COUNTERPARTY")
@Entity(name = "mm_Counterparty")
@DiscriminatorValue("COUNTERPARTY")
@NamePattern("%s|name")
public class Counterparty extends StandardEntity {

@Column(name = "TYPE", insertable=false, updateable=false)
private String type;

...otherColumn

我需要获取所有条件为 monitoringCard 的示例 - 1) 在开始日期和结束日期之间 2) 交易对手类型 = 'someType'

我做方法-

List<MonitoringCard> monitoringCardList = dataManager.load(MonitoringCard.class)
.query("select distinct cm from mm_MonitoringCard m join mm_Counterparty cm where (m.current_date between cm.startDate and cm.endDate) and cm.type = :type")
.parameter("type", "someType")
.list();

但是我得到了错误,我怎样才能做出正确的请求? 谢谢

您的查询不正确。它应该使用这样的连接:

select distinct m.Counterparty from mm_MonitoringCard m 
where (m.current_date between m.Counterparty.startDate and m.Counterparty.endDate) 
  and m.Counterparty.type = :type

在 JPA 中,在 ToOne 关系上导航时不需要进行显式连接。