【问题标题】:SpringData Repository method returning empty collectionSpring Data Repository 方法返回空集合
【发布时间】:2016-07-20 09:12:20
【问题描述】:

我有一个存储库,其中定义了一个方法:

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/ 一切似乎都很好。

谁能帮帮我,我做错了什么?

【问题讨论】:

  • 我认为您的查询findByBillBillNumber 不正确。您正在尝试访问 Bill 对象内的 BillNumber 字段。您是否尝试过定义 BillRepository?并在这个新存储库中定义查询findByBillNumber
  • 我已经拥有BillRepository,但没有定义方法findByBillBillNumber(因为它不需要IMO,因为我在其他控制器中为UserRepository和方法定义做同样的事情在这种情况下不需要,只定义了存储库,并且它工作正常)。我仍然尝试添加该方法,但没有任何收获。

标签: hibernate repository spring-data-jpa one-to-many many-to-one


【解决方案1】:

问题在于您的数据。您有相同 ID 的不同付款,当您执行保存的方法时,您只插入一笔付款,然后用其余的账单更新它。因此,您的 find 方法无法获得与账单 1 匹配的任何付款,因为 db 中只有一笔付款,其账单为 12。

除此之外,bill number 是你的 ID,可能最好使用 findByBill 作为查询方法和 Bill 对象作为参数。

【讨论】:

  • 啊,我错过了。复制粘贴数据,忘记更新了。我的错。为每一行提供唯一的键,现在它很完美。刚刚触动我的一件事:save() 不是典型的数据库插入操作,它更多的是更新。如果它像插入它必须在第二个save() 调用中抛出错误,因为“唯一约束违反”。不是吗?
  • Save 方法将保留或合并您的给定实体,具体取决于它是否存在。您可以在JPA Repository Documentation - 第 2.2.1 节 中找到有关它的更多信息
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多