返回空集合的 Spring Data Repository 方法
SpringData Repository method returning empty collection
我有一个存储库,其中定义了一个方法:
public interface BillPaymentRepository extends JpaRepository<BillPayment, String>{
Collection<BillPayment> findByBillBillNumber(String billNo);
}
相关实体如下:
@Entity
public class BillPayment {
@Id
private String transactionNumber;
@ManyToOne
private Bill bill;
private Float amountPaid;
public String getTransactionNumber() {
return transactionNumber;
}
public Bill getBill() {
return bill;
}
public Float getAmountPaid() {
return amountPaid;
}
public BillPayment(String transactionNumber, Bill bill, Float amountPaid) {
this.transactionNumber = transactionNumber;
this.bill = bill;
this.amountPaid = amountPaid;
}
public BillPayment() {
}
}
另一个实体:
@Entity
public class Bill {
@Id
private String billNumber;
@OneToOne
private User user;
private Month billMonth;
private Float billAmount;
private Float dataUtilized;
public String getBillNumber() {
return billNumber;
}
public User getUser() {
return user;
}
public Month getBillMonth() {
return billMonth;
}
public Float getBillAmount() {
return billAmount;
}
public Float getDataUtilized() {
return dataUtilized;
}
public Bill(String billNumber, User user, Month billMonth, Float billAmount, Float dataUtilized) {
this.billNumber = billNumber;
this.user = user;
this.billMonth = billMonth;
this.billAmount = billAmount;
this.dataUtilized = dataUtilized;
}
public Bill() {
}
}
以下是相关仓库中填写的数据:
Bill bill1 = billRepository.save(new Bill("B1", user1, Month.APRIL, 10F, 10F));
Bill bill2 = billRepository.save(new Bill("B2", user2, Month.APRIL, 60F, 60F));
Bill bill3 = billRepository.save(new Bill("B3", user3, Month.APRIL, 50F, 50F));
Bill bill4 = billRepository.save(new Bill("B4", user4, Month.APRIL, 20F, 20F));
Bill bill5 = billRepository.save(new Bill("B5", user1, Month.MAY, 30F, 30F));
Bill bill6 = billRepository.save(new Bill("B6", user2, Month.MAY, 30F, 30F));
Bill bill7 = billRepository.save(new Bill("B7", user3, Month.MAY, 40F, 40F));
Bill bill8 = billRepository.save(new Bill("B8", user4, Month.MAY, 10F, 10F));
Bill bill9 = billRepository.save(new Bill("B9", user1, Month.JUNE, 10F, 10F));
Bill bill10 = billRepository.save(new Bill("B10", user2, Month.JUNE, 10F, 10F));
Bill bill11 = billRepository.save(new Bill("B11", user3, Month.JUNE, 50F, 60F));
Bill bill12 = billRepository.save(new Bill("B12", user4, Month.JUNE, 20F, 20F));
billRepository.save(new Bill("B13", user1, Month.JULY, 10F, 10F));
billRepository.save(new Bill("B14", user2, Month.JULY, 30F, 30F));
billRepository.save(new Bill("B15", user3, Month.JULY, 15F, 0F));
billRepository.save(new Bill("B16", user4, Month.JULY, 10F, 10F));
billPaymentRepository.save(new BillPayment("T1", bill1, 10F));
billPaymentRepository.save(new BillPayment("T1", bill2, 10F));
billPaymentRepository.save(new BillPayment("T1", bill3, 10F));
billPaymentRepository.save(new BillPayment("T1", bill4, 10F));
billPaymentRepository.save(new BillPayment("T1", bill5, 10F));
billPaymentRepository.save(new BillPayment("T1", bill6, 10F));
billPaymentRepository.save(new BillPayment("T1", bill7, 10F));
billPaymentRepository.save(new BillPayment("T1", bill8, 10F));
billPaymentRepository.save(new BillPayment("T1", bill9, 10F));
billPaymentRepository.save(new BillPayment("T1", bill10, 10F));
billPaymentRepository.save(new BillPayment("T1", bill11, 10F));
billPaymentRepository.save(new BillPayment("T1", bill12, 10F));
但是当我调用方法获取结果时,它返回空集合。
@RestController
@RequestMapping("/payment-details")
class BillPaymentRestController {
private final BillPaymentRepository billPaymentRepository;
@Autowired
public BillPaymentRestController(BillPaymentRepository billPaymentRepository) {
this.billPaymentRepository = billPaymentRepository;
}
@RequestMapping(method=RequestMethod.GET)
Collection<BillPayment> getPaymentDetailsByBillNo(@RequestParam String billNo){
/*System.out.println(billNo);
for (BillPayment billPayment : this.billPaymentRepository.findByBillBillNumber(billNo)) {
System.out.println(billPayment.getTransactionNumber());
}
System.out.println(this.billPaymentRepository.findByBillBillNumber(billNo));*/
return this.billPaymentRepository.findByBillBillNumber(billNo);
}
}
我正在使用 POSTMAN 通过 GET 请求测试服务:
localhost:8080/payment-details?billNo=B1
我在同一个项目中有其他具有类似功能的服务,它们运行良好。
它们和这个唯一的区别是实体之间的关系。
所以我阅读了 OneToMany 和 ManyToOne 关系。根据本教程:
https://howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/
一切似乎都很好。
谁能帮帮我,我做错了什么?
问题出在你的数据上。您有具有相同 ID 的不同付款,当您执行保存方法时,您只插入一笔付款,然后用其余账单更新它。因此,您的查找方法无法获得与账单 1 匹配的任何付款,因为数据库中只有一笔付款的账单是 12.
除此之外,bill number 就是你的 ID,可能使用 findByBill 作为查询方法和 Bill 对象作为参数会更好。
我有一个存储库,其中定义了一个方法:
public interface BillPaymentRepository extends JpaRepository<BillPayment, String>{
Collection<BillPayment> findByBillBillNumber(String billNo);
}
相关实体如下:
@Entity
public class BillPayment {
@Id
private String transactionNumber;
@ManyToOne
private Bill bill;
private Float amountPaid;
public String getTransactionNumber() {
return transactionNumber;
}
public Bill getBill() {
return bill;
}
public Float getAmountPaid() {
return amountPaid;
}
public BillPayment(String transactionNumber, Bill bill, Float amountPaid) {
this.transactionNumber = transactionNumber;
this.bill = bill;
this.amountPaid = amountPaid;
}
public BillPayment() {
}
}
另一个实体:
@Entity
public class Bill {
@Id
private String billNumber;
@OneToOne
private User user;
private Month billMonth;
private Float billAmount;
private Float dataUtilized;
public String getBillNumber() {
return billNumber;
}
public User getUser() {
return user;
}
public Month getBillMonth() {
return billMonth;
}
public Float getBillAmount() {
return billAmount;
}
public Float getDataUtilized() {
return dataUtilized;
}
public Bill(String billNumber, User user, Month billMonth, Float billAmount, Float dataUtilized) {
this.billNumber = billNumber;
this.user = user;
this.billMonth = billMonth;
this.billAmount = billAmount;
this.dataUtilized = dataUtilized;
}
public Bill() {
}
}
以下是相关仓库中填写的数据:
Bill bill1 = billRepository.save(new Bill("B1", user1, Month.APRIL, 10F, 10F));
Bill bill2 = billRepository.save(new Bill("B2", user2, Month.APRIL, 60F, 60F));
Bill bill3 = billRepository.save(new Bill("B3", user3, Month.APRIL, 50F, 50F));
Bill bill4 = billRepository.save(new Bill("B4", user4, Month.APRIL, 20F, 20F));
Bill bill5 = billRepository.save(new Bill("B5", user1, Month.MAY, 30F, 30F));
Bill bill6 = billRepository.save(new Bill("B6", user2, Month.MAY, 30F, 30F));
Bill bill7 = billRepository.save(new Bill("B7", user3, Month.MAY, 40F, 40F));
Bill bill8 = billRepository.save(new Bill("B8", user4, Month.MAY, 10F, 10F));
Bill bill9 = billRepository.save(new Bill("B9", user1, Month.JUNE, 10F, 10F));
Bill bill10 = billRepository.save(new Bill("B10", user2, Month.JUNE, 10F, 10F));
Bill bill11 = billRepository.save(new Bill("B11", user3, Month.JUNE, 50F, 60F));
Bill bill12 = billRepository.save(new Bill("B12", user4, Month.JUNE, 20F, 20F));
billRepository.save(new Bill("B13", user1, Month.JULY, 10F, 10F));
billRepository.save(new Bill("B14", user2, Month.JULY, 30F, 30F));
billRepository.save(new Bill("B15", user3, Month.JULY, 15F, 0F));
billRepository.save(new Bill("B16", user4, Month.JULY, 10F, 10F));
billPaymentRepository.save(new BillPayment("T1", bill1, 10F));
billPaymentRepository.save(new BillPayment("T1", bill2, 10F));
billPaymentRepository.save(new BillPayment("T1", bill3, 10F));
billPaymentRepository.save(new BillPayment("T1", bill4, 10F));
billPaymentRepository.save(new BillPayment("T1", bill5, 10F));
billPaymentRepository.save(new BillPayment("T1", bill6, 10F));
billPaymentRepository.save(new BillPayment("T1", bill7, 10F));
billPaymentRepository.save(new BillPayment("T1", bill8, 10F));
billPaymentRepository.save(new BillPayment("T1", bill9, 10F));
billPaymentRepository.save(new BillPayment("T1", bill10, 10F));
billPaymentRepository.save(new BillPayment("T1", bill11, 10F));
billPaymentRepository.save(new BillPayment("T1", bill12, 10F));
但是当我调用方法获取结果时,它返回空集合。
@RestController
@RequestMapping("/payment-details")
class BillPaymentRestController {
private final BillPaymentRepository billPaymentRepository;
@Autowired
public BillPaymentRestController(BillPaymentRepository billPaymentRepository) {
this.billPaymentRepository = billPaymentRepository;
}
@RequestMapping(method=RequestMethod.GET)
Collection<BillPayment> getPaymentDetailsByBillNo(@RequestParam String billNo){
/*System.out.println(billNo);
for (BillPayment billPayment : this.billPaymentRepository.findByBillBillNumber(billNo)) {
System.out.println(billPayment.getTransactionNumber());
}
System.out.println(this.billPaymentRepository.findByBillBillNumber(billNo));*/
return this.billPaymentRepository.findByBillBillNumber(billNo);
}
}
我正在使用 POSTMAN 通过 GET 请求测试服务:
localhost:8080/payment-details?billNo=B1
我在同一个项目中有其他具有类似功能的服务,它们运行良好。 它们和这个唯一的区别是实体之间的关系。 所以我阅读了 OneToMany 和 ManyToOne 关系。根据本教程: https://howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/ 一切似乎都很好。
谁能帮帮我,我做错了什么?
问题出在你的数据上。您有具有相同 ID 的不同付款,当您执行保存方法时,您只插入一笔付款,然后用其余账单更新它。因此,您的查找方法无法获得与账单 1 匹配的任何付款,因为数据库中只有一笔付款的账单是 12.
除此之外,bill number 就是你的 ID,可能使用 findByBill 作为查询方法和 Bill 对象作为参数会更好。