SpringData Repository方法返回空集合

时间:2016-07-20 09:12:20

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

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

HKSamples

相关实体如下:

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));

我使用POSTMAN使用GET请求测试服务:

@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);
    }
}

我在同一个项目中有其他类似功能的服务正常运行。 它们和唯一的区别在于实体之间的关系。 所以我读到了OneToMany和ManyToOne realationaships。根据本教程: http://www.mathworks.com/help/images/ref/watershed.html 一切似乎都很好。

有人可以帮帮我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

除此之外,账单号码是您的ID,使用findByBill作为查询方法和Bill对象作为参数可能会更好。