I have this spring serviceImp method
public DemographicResponse getDemographics(IDVBO idvBo) throws Exception {
		Boolean isSuccess = false;
		logger.info("Inside IDVServiceImpl getDemographics");
		IDVTransaction transaction = fetchTransactionDetails(idvBo,
				String.valueOf(idvBo.getTransactionBO().getAckReferenceNo()));

		String sourceSountry = idvBo.getTransactionBO().getSourceCountry();
		DemographicResponse main_res = new DemographicResponse();
		IDVExternalPluginService service = plugInService(sourceSountry);

		if (config.isDemographyEnabled(sourceSountry)) {
			if (isCustomerDataAlreadyNotRetrived(transaction)) {
				try {
					idvBo = service.getDemographics(idvBo);
					isSuccess = true;
				} finally {
					persistDemographicDetails(idvBo, isSuccess);
				}
				
			} else {
				idvBo = buildIDVBOFromDB(idvBo, transaction);
			}
			IDVBOToDemographicResponse converter = new IDVBOToDemographicResponse();
			main_res = converter.convert(idvBo);
		}

		logger.info("Exiting IDVServiceImpl getDemographics");
		return main_res;
	}
	
For reference
private void persistDemographicDetails(IDVBO idvBo, Boolean isSuccess) throws DatabaseConnectionErrorException {
		try {
			EkycGoldenSourcePayload idvEkycGoldenSourcePayload = ekycGoldenSourcePayloadRepo
					.saveAndFlush(entityConverter.IDVBOToGoldenSourcePayload(idvBo));
			idvBo.getTransactionBO().setIdvEkycGoldenSourcePayloadId(idvEkycGoldenSourcePayload.getId());
			logger.info("Persisted IDVEkycGoldenSourcePayload entity");
			IDVEkycGoldenSource idvEkycGoldenSourceExtracted = idvEkycGoldenSourceRepo
					.saveAndFlush(entityConverter.IDVBOToGoldenSourceExtracted(idvBo));
			idvBo.getTransactionBO().setIdvEkycGoldenSourceId(idvEkycGoldenSourceExtracted.getId());
			logger.info("Persisted IDVEkycGoldenSource entity");
			CustomerInformation customerInformation = customerInformationRepo
					.saveAndFlush(entityConverter.IDVBOToCustomerInformation(idvBo));
			idvBo.getTransactionBO().setIdvCustomerInformationId(customerInformation.getId());
			logger.info("Persisted CustomerInformation entity");
			if (isSuccess) {
				updateState(idvBo, CUSTOMER_DATA_RETRIVED.name());
			}
			repository.save(entityConverter.IDVBOTransaction(idvBo));
			logger.info("Persisted Transaction entity");
		} catch (CannotCreateTransactionException e) {
			logger.error("Database connection error. : " + e);
			throw new DatabaseConnectionErrorException(idvBo.getTransactionBO().getTransactionReference(), "");
		}
	}

When calling the getDemographics API (for a failed scenario where the service.getDemographics failes due to a vendor call - example {"error_details":[{"id":"TXNREF10011735799818","code":"IDV200_TP_INVALID_ARGUMENT","detail":"Downstream System Error","source":"Zoloz : Bad Response. The invalid field attributes [ISSUE_DATE]","action":"ReSubmit"}]})

I have added an idempotency feature in the getDemographics method where when recalling the same API with same request body I should always get the same response even if it is a failure response.

But for this condition I get this as a response when calling the API for the second time after a failure with the same request body
{"error_details":[{"id":"","code":"IDV999","detail":"Unexpected error occurred","source":"could not execute statement [ERROR: duplicate key value violates unique constraint \"idv_ekyc_golden_source_payload_ack_index\"\n  Detail: Key (ack_reference_no)=(IDV-HK-01-20250102120659-551b067d-f54d-4cef-b) already exists.] [insert into idv_ekyc_golden_source_payload (ack_reference_no,channel_reference,created_date,fingerprint_match_golden_source_data,image_match_golden_souce_data,poa_golden_source_data,poi_golden_source_data,txn_reference_no,idv_ekyc_golden_source_payload_id) values (?,?,?,?,?,?,?,?,?)]; SQL [insert into idv_ekyc_golden_source_payload (ack_reference_no,channel_reference,created_date,fingerprint_match_golden_source_data,image_match_golden_souce_data,poa_golden_source_data,poi_golden_source_data,txn_reference_no,idv_ekyc_golden_source_payload_id) values (?,?,?,?,?,?,?,?,?)]; constraint [idv_ekyc_golden_source_payload_ack_index]","action":"ReSubmit"}]}

How to fix it?