I have a springBoot 2.1.9.RELEASE application that uses Spring Data for Couchbase
I have this object
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
@NotNull
@JsonProperty("_location")
private T location;
}
and this other one
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder
public class HTMDoc {
@Id
private String id;
@NotNull
@Field
private Hostel hostel;
}
on the service
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
on the test
service.create(new HTMDoc());
but when I save I got this error instead of the validation NotNull in the hostel field
org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.
You need to use the @org.springframework.validation.annotation.Validated
annotation over your service class to enable validation.
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With