使用OneToOne懒惰的JPA查询

时间:2016-06-01 02:06:04

标签: jpa spring-data-jpa

我有这个对象的结构

@Entity
public class Member {

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

    private String name;

    private boolean man;

    private String address;

    @OneToOne(fetch = FetchType.LAZY)
    private City city;

    private String postalCode;

    private String phone1;

    private String phone2;

    private LocalDate birthdate;

    private String email;

    private String emergencyContactName;

    private String emergencyPhone;

    private String paymentGatewayKey;

    @OneToMany(fetch = FetchType.LAZY)
    private List<Contract> contracs;

    @OneToOne(fetch = FetchType.LAZY)
    private Commerce commerce;
}


@Entity
public class Contract {

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

    private BigDecimal price;

    private int frequency;

    private int term;

    private LocalDate startDate;

    private LocalDate endDate;

    private int numberOfPayment;

    @Enumerated(EnumType.STRING)
    private StatusEnum status;

    @OneToMany(fetch = FetchType.LAZY,mappedBy = "contract")
    private List<Payment> payments;

    @ManyToOne
    private Member member;
}

@Entity
public class Payment {

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

    private BigDecimal price;

    private LocalDate date;

    @Enumerated(EnumType.STRING)
    private StatusEnum status;

    @Enumerated(EnumType.STRING)
    private PaymentModeEnum paymentMode;

    @ManyToOne
    private Contract contract;

    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    private List<Operation> operations;

}

是否可以从会员查询中获取所需的合同,付款,城市和商业信息?

如果会员有很多合同......我只想获得合同#2 ......

我开始这个查询但是城市和商业很懒,我不知道如何处理这些字段。

select m from Member m inner join fetch m.contracs c inner join fetch c.payments p where c.contractId = :contractId

0 个答案:

没有答案