条件查询连接

时间:2015-12-08 18:13:39

标签: jpa spring-data-jpa

我有一个Rent类,其中包含付款清单。

我想找到使用paymentDue或空付款

的租金

因此,如果租金没有任何支付,我想得到它。 另外如果有roomPayment且包含paymentDueDate且参数中的日期在paymentDueDate之后,如果之后没有其他roomPayment,我想得到它。

是否可以在JPA中执行此操作,或者我需要获取数据并使用Java解析它?

@Entity
public class Rent {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long rentId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "rent")
    private List<RoomPayment> roomPaymentList;

    private LocalDate fromDate;

    private LocalDate toDate;
    ...
}

@Entity
public class RoomPayment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long roomPaymentId;

    @OneToOne
    private PaymentType paymentType;

    private BigDecimal amountReceived;

    private LocalDate paymentDueDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rent_id")
    private Rent rent;
    ...
}

我创建了此查询,但它不完整

@Query("select r from Rent r where r.roomPaymentList is empty and r.fromDate <= :date and r.toDate >= :date")
public List<Rent> findLateRentPayment(@Param("date") LocalDate date);

修改

实际上,此查询会生成此

select
    rent0_.rent_id as rent_id1_33_,
    rent0_.bail_id as bail_id4_33_,
    rent0_.from_date as from_dat2_33_,
    rent0_.to_date as to_date3_33_ 
from
    rent rent0_ 
where
    not (exists (select
        roompaymen1_.room_payment_id 
    from
        room_payment roompaymen1_ 
    where
        rent0_.rent_id=roompaymen1_.rent_id)) 
    and rent0_.from_date<=? 
    and rent0_.to_date>=?

如果我有这些数据

rent
rentId | fromDate | toDate
1      | 2015-12-08 | 2015-12-15
2      | 2015-12-08 | 2015-12-15
3      | 2015-12-08 | 2015-12-15

roompayment

roomPaymentId   | rentId    |   paymentDueDate
1               | 1         |
3               | 3         |   2015-12-14  

查询应返回2和3

2因为租金没有任何roomPaymentId 3因为有一个roomDayment with paymentDueDate但他们之后没有任何其他roomPayment

1 个答案:

答案 0 :(得分:0)

 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973209   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973294   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973376   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973482   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973494   26410 proxysocket.go:134] Failed to connect to balancer: failed to connect to an endpoint.

It will return rents containing no payment and rents containing any payment which the due date is before a parameter date. Don't know if this is the result set that you want.