Использование пружинной загрузки с JPA:
пытаюсь сделать обновление таблицы. но получаю ошибку при обновлении значения поля NaturalId.
ERROR:nested exception is org.hibernate.HibernateException: An immutable natural identifier of entity com.model.Company was altered from SOL to MEP
Модель Entity:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "COMPANY_ID")
private Long companyId;
@NotBlank
@NaturalId
@Size(max = 5)
@Column(name = "COMPANY_CODE")
private String companyCode;
Контроллер и сервис:
@PutMapping("/company")
public ResponseEntity<?> updateCompany(@Valid @RequestBody CompanyRequest companyRequest) {
logger.info("UPDATE COMPANY : {} ", companyRequest);
Company company = companyService.updateCompany1(companyRequest);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{companyId}")
.buildAndExpand(company.getCompanyId()).toUri();
return ResponseEntity.created(location)
.body(new ApiResponse(true, "Company Updated Successfully"));
}
//Services:
public Company updateCompany1(CompanyRequest companyRequest) {
Company company = companyRepository.findById(companyRequest.getCompanyId())
.orElseThrow(() -> new ResourceNotFoundException("Company", "companyId", companyRequest.getCompanyId()));
company.setCompanyCode(companyRequest.getCompanyCode());
return companyRepository.saveAndFlush(company);
}