gpt4 book ai didi

java - 无法更新 NaturalId 字段值 : An immutable natural identifier of entity {Model} was altered from ABC to XYZ

转载 作者:行者123 更新时间:2023-12-02 01:26:08 27 4
gpt4 key购买 nike

将 spring-boot 与 JPA 结合使用:尝试更新表。但当我更新 NaturalId 字段值时出现错误。

错误:嵌套异常为 org.hibernate.HibernateException:实体 com.model.Company 的不可变自然标识符已从 SOL 更改为 MEP

模型实体:

  @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "COMPANY_ID")
private Long companyId;

@NotBlank
@NaturalId
@Size(max = 5)
@Column(name = "COMPANY_CODE")
private String companyCode;

Controller 和服务:

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

最佳答案

1) 在 NaturalId 上设置可变属性,2) 使您的列独一无二:

您的映射应如下所示:

@NotBlank
@NaturalId(mutable=true)
@Size(max = 5)
@Column(name = "COMPANY_CODE", unique=true)
private String companyCode;

关于java - 无法更新 NaturalId 字段值 : An immutable natural identifier of entity {Model} was altered from ABC to XYZ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56882858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com