spring,jpa:如何使用多对多关系桥表设置查询另一个实体下的实体

vxf3dgd4  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(365)

我对Spring还相当陌生。我正在用这个erd查询一个捐赠者下的所有捐赠:
捐赠者|-----协议*-----捐赠(使用桥接表的多对多关系)
这是我的密码:
捐赠者.java

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Donor extends Auditable implements Comparable<Donor>{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "Cannot have an empty account number field.")
    private String accountNumber;
    private String accountName;
    private String salutation;
    private String donorName;
    private String cellphoneNumber;
    private String emailAddress;
    private String companyTIN;
    private String phone1;
    private String phone2;
    private String faxNumber;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String address5;
    private String companyAddress;
    private LocalDate birthDate;
    private String notes;

    @OneToMany(mappedBy = "donor")
    List<MOA> moaList = new ArrayList<>();
    ...
}

捐赠.java

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Donation extends Auditable implements Comparable<Donation> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotBlank(message = "Cannot have an empty account number field.")
    private String accountNumber;
    private String accountName;
    private String orNumber;
    private String date;
    private Double amount;
    private String notes;
    private String needCertificate;
    private String purposeOfDonation;

    @OneToMany(mappedBy = "donation")
    List<MOA> moaList = new ArrayList<>();
    ...
}

moa.java(协议)

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MOA extends Auditable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "donor_id")
    @JsonIgnoreProperties("moaList")
    private Donor donor;

    @ManyToOne
    @JoinColumn(name = "donation_id")
    @JsonIgnoreProperties("moaList")
    private Donation donation;

    private String name;
    private String donorAccountNumber;
    private Long foreignDonationId;
    private LocalDate dateSigned;
}

在我的donorrepository中,我试图提出这样一个问题,我希望它能给我想要的东西:

public interface DonorRepository extends JpaRepository<Donor, Long> {
    ...
    @Query(value = "SELECT * FROM donor WHERE account_number = ?1", nativeQuery = true)
    List<Donation> findDonorsDonations(String accountNumber);
    ...

这给了我一个错误
无法将值{1,admin,2021-04-01 10:29:53.0,admin,2021-04-01 10:29:53.0,school,123456,null,null,null,null,null,null,john doe,null,null,null,null,null,null,mr.}从类型[java.lang.object[]]转换为类型[com.package.server.domain.generation];嵌套异常为org

z18hc3ub

z18hc3ub1#

您可以使用specification api和specificationexecutor。
你必须加入mao捐赠(mao与捐赠者),然后查询特定捐赠者的捐赠。

相关问题