所以我遇到这个问题与更新的实体数据库。 同时穿整个实体和更新,只有特定的领域对待触及的领域的空,其结果我得到的一个例外,因为这些领域是 @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
为创造,但是,我需要更新的实体,有时只有特定的领域。
服务:
@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
已经通过这会导致其余的属性可以自动空,如果不修改。
控制器:
@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}'}
]
谢谢。