Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax validation does not validate notNull

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.
like image 688
Sandro Rey Avatar asked Dec 22 '22 21:12

Sandro Rey


1 Answers

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);
  }
}
like image 120
George Avatar answered Jan 05 '23 16:01

George