У меня есть хранилище следующим образом с методом, определенным в нем:метод SpringData Repository возвращается пустая коллекция
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);
}
}
Я использую Почтальон протестировать сервис с использованием запроса GET:
localhost:8080/payment-details?billNo=B1
У меня есть другие услуги, имеющие аналогичные такие функциональные возможности в том же проекте, которые работают отлично. Единственная разница в них и в этой связи была связана между сущностями. Итак, я прочитал о OneToMany и ManyToOne realationaships. И согласно этому руководству: https://howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/ все кажется прекрасным.
Может кто-то, пожалуйста, помогите мне, что я делаю неправильно?
Я думаю, что ваш запрос 'findByBillBillNumber' неверен. Вы пытаетесь получить доступ к полю BillNumber внутри объекта Bill. Вы пытались определить BillRepository? И определить запрос 'findByBillNumber' внутри этого нового репозитория? – amicoderozer
У меня уже есть 'BillRepository', но метод' findByBillBillNumber' не был определен (поскольку он не нужен IMO, поскольку я делаю то же самое в других контроллерах для 'UserRepository', и определение метода в этом случае не требуется, только репозиторий, и его рабочий штраф). Тем не менее я попытался добавить метод и не получить. –