Home > database >  Error with saving Json with a nested Object [query did not return a unique result: 2; nested excepti
Error with saving Json with a nested Object [query did not return a unique result: 2; nested excepti

Time:11-17

I have a json file that has some parmeters with a nested object in it. And I'm trying to save this json file to the database. Altough I keep reciving this error:

" query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2"

`

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

`

So to insert an object in my DTO that has an array of (Invoices) I created an Object name invoices of type InvoicesDTO, then I tried to itirate through these Object everytime a request will be reviced using this API. But Still I get this error.

the dto's for Invoice and invoices:

`

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

CodePudding user response:

Either

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

or

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

returns more than one rows. Check the tables, please

CodePudding user response:

You get this exception if you query returns more than one result and you expect that will returns one result

So check if you have more than line that has personId = 1xxxxxxx

  • Related