그래서 저는 이 문제가 발생한 업데이트와는 엔티티에서 DB. 을 통과하면서 전체 엔터티 및 업데이트만의 특정 필드를 처리 손길이 닿지 않은 분야로 null,결과적으로 나는 예외가 이런 분야 @Not-Null
,
가 찾는 유사한 문제가지 문제 해결 문제입니다.
회사 개체:
@Entity
@Table (name = "companies")
@Data
@ToString(exclude = "perfumes")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotNull
private String name;
@NotNull
@Email(message = "Wrong input. please enter a VALID email address")
private String email;
@NotNull
@Size(min = 4, max = 14, message = "Password range must be between 4 - 14 digits")
private String password;
@NotNull
@Enumerated(EnumType.STRING)
private Country country;
@Singular
@OneToMany(mappedBy = "company", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Perfume> perfumes = new ArrayList<>();
}
대부분의 필드는 @NotNull
생성,그러나,를 업데이트해야 합 entity,때로는 특정한 필드가 있습니다.
서비스:
@Override
public String updateCompany(int id, Company company) throws DoesNotExistException {
if(!companyRepository.existsById(id))
{
throw new DoesNotExistException(id);
}
companyRepository.saveAndFlush(company);
return company.getName() + " has been UPDATED";
}
당신이 볼 수 있습니다 ENTITY
전달되었는 원인은 나머지 부분의 속성을 자동으로 null 인 경우 수정되지 않습니다.
컨트롤러:
@PutMapping("/updateCompany/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public String updateCompany(@PathVariable int id, @RequestBody Company company) throws DoesNotExistException {
return admin.updateCompany(id,company);
}
예외:
Validation failed for classes [com.golden.scent.beans.Company] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=password, rootBeanClass=class com.golden.scent.beans.Company, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
감사합니다.