JPA如何加入这些实体

鉴于下面的JPA实体,我希望获得具有至少一个成功状态的请求的所有Debits。

可以有许多具有相同debit_id和不同状态的请求

我应该使用这样的东西,还是有更好的办法

entityManager.createQuery(“从借记d中选择c加入d.id,其中request.status =成功”

@Entity(name = "T_DEBIT") public class Debit { public enum Status { NEW, OLD } @Column(name = "STATUS", nullable = false, length = 20) @Enumerated(value = EnumType.STRING) private Status status; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Long id; @ManyToOne(optional = false) @JoinColumn(name = "ACCOUNT_ID", updatable = false, nullable = false) private Account account; } 

和其他实体是

 @Entity(name = "T_REQUEST") public class Request{ public enum Status { PENDING, FAILED, SUCCESFUL} @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Long id; @ManyToOne(optional = false) @JoinColumn(name = "DEBIT_ID", updatable = false, nullable = false) private Debit debit; @Column(name = "STATUS", nullable = false, length = 20) @Enumerated(value = EnumType.STRING) private Status status; } 

如果遗漏任何内容而不是关闭或低估问题,请发表评论!

基本上:

 select d from T_DEBIT d where exists ( select r from T_REQUEST r where r.debit.id = d.id and r.status = SUCCESSFUL ) 

检查JPQL中的枚举语法,我通常不使用实体的枚举,在这个例子中它可能是错误的。

作为样式问题,我会使实体名称==类名而不是实体名==表名。 这使得JPQL 不是 SQL更清晰

UPDATE

Spring要求解决类似的问题。 解决这些问题的方法非常系统:

a)仅使用基本filter和以下表达式重写您的问题:

  1. “存在一些……这样的条件是真的”
  2. “为了所有……条件是真的”

b)翻译:

  1. 这种情况变得exists (select ... where condition)
  2. 这种情况not exists (select ... where NOT condition)

在Spring的特定问题中,“排除所有成功请求”,目标不是很明确。 如果他/她的意思是“在没有成功请求的情况下获得所有借记”,那么您会这样做:

a)将问题重写为“获取所有借记,以便对所有相关请求,请求状态不成功 ”。 b)翻译为

 select d from T_DEBIT d where not exists ( select r from T_REQUEST r where -- This is the join condition, so it should not be negated r.debit.id = d.id and -- This is the actual filtering condition, negate as this is a FOR ALL not (r.status != SUCCESSFUL) ) 

然后你可以简化最后一个条件,得到:

 select d from T_DEBIT d where not exists ( select r from T_REQUEST r where r.debit.id = d.id and r.status = SUCCESSFUL )