我必须建立模型,即paymentRecon和运单。一个PaymentRecon可以在他的下方设置一组运单。但不想将运单与其紧密结合。所以我创建如下模型:

paymentRecon

@SuppressWarnings("serial")
@Entity
public class PaymentReconciliation extends BaseEntity {
@Column
private String parentId;
@Column
private BigDecimal grossAmount;
@Column
private String currency;
@Column
private Integer totalNumberOfPackages;
@Column
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "PayReconWaybillMap", joinColumns = {
@JoinColumn(name = "reconId") }, inverseJoinColumns = {
        @JoinColumn(name = "waybillId") })
private Set<WayBill> waybill = new HashSet<WayBill>();
}


ab

 @SuppressWarnings("serial")
 @Entity
 public class PaymentReconciliation extends BaseEntity {
 @Column(nullable = false, unique = true)
 private String barCode;
  @Column
  private String consigneeName;
  @Column
  @JsonIgnore
  private String countryCode;

 }


现在,我有三个桌子。
因此,开发完成之后。我需要获得未附在任何PaymentRecon上的运单。有人可以帮我怎么做吗?

最佳答案

您可以使用PayReconWaybillMap上的@ManyToOne引用此中间实体,从而在新实体PaymentReconciliation上映射“联接表”:

@ManyToOne
private PayReconWaybillMap pwm;


PayReconWaybillMap实体中:

@OneToMany
private List<PaymentReconciliation> pr;

@OneToMany
private List<WayBill> wb;


因此,您可以使用JOIN,如下所示:

SELECT wb FROM WayBill wb
LEFT JOIN PayReconWaybillMap pwm ON pwm.waybillId = wb.waybillId
WHERE pwm IS NULL

10-06 01:08