使用嵌套对象保存Json时错误[查询没有返回唯一结果:2;嵌套异常是javax.persistence.NonUniqu



我有一个json文件,其中有一个嵌套对象的一些参数。我试图将这个json文件保存到数据库中。虽然我一直收到这个错误:

"查询没有返回唯一结果:2;嵌套异常是javax.persistence.NonUniqueResultException:查询没有返回唯一的结果:2">

{
"personId" : "1xxxxxxx",
"invoiceDate" : "2020-10-12",
"invoices":[
{
"invoiceAmount" :"300",
"invoiceNumber" :"x123"
},
{
"invoiceAmount" :"100",
"invoiceNumber" :"x122"
}
],   
}

因此,为了在我的DTO中插入一个具有(发票)数组的对象,我创建了一个类型为InvoicesDTO的对象名称发票,然后我尝试在每次使用此API修改请求时通过这些对象进行初始化。但是我仍然得到这个错误。

发票和发票的日期:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InvoiceDTO {
@NotNull(message = "PersonId can't be null")
@PersonId
private String personId;
@NotNull(message = "invoiceDate can't be null")
// @DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd"))
@PastOrPresent
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate invoiceDate;
private InvoicesDTO invoices;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class InvoicesDTO implements List<InvoicesDTO> {
@NotNull(message = "invoice Number can't be null")
private String invoiceNumber;
@NotNull(message = "invoiceAmount can't be null")
private Double invoiceAmount; 

try {
//Add the new amount of the invoice to an existing debts
Optional<Debts> debts = debtsRepository.findDebtsByPersonIdAndOrganization_id(invoiceDTO.getPersonId(),organization.get().getId());
Optional<Madeen> madeenOptional = madeenRepository.findByPersonId(invoiceDTO.getPersonId());
List<InvoicesDTO> invoicesDTO = invoiceDTO.getInvoices();
for (InvoicesDTO invoices : invoicesDTO) {
Debts newDebt = new Debts(); //Only debts
newDebt.setPersonId(invoiceDTO.getPersonId());
newDebt.setCreatedDate(LocalDate.now());
newDebt.setUpdatedDate(invoiceDTO.getInvoiceDate());
newDebt.setDebtAmount(invoices.getInvoiceAmount());
newDebt.setInvoiceNumber(invoices.getInvoiceNumber());
newDebt.setOrganization(organization.get());
debtsRepository.save(newDebt);
}

或者

Optional<Debts> debts = debtsRepository.findDebtsByPersonIdAndOrganization_id(invoiceDTO.getPersonId(),organization.get().getId());

Optional<Madeen> madeenOptional = madeenRepository.findByPersonId(invoiceDTO.getPersonId());

返回不止一行。请检查一下桌子

如果查询返回多个结果并且期望返回一个结果,则会得到此异常

检查是否有超过一行personId = 1xxxxxxx