5

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.
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • 1
    The id field does not have _@NotNull, why it should validate it? I would put it there and I would consider to apply the _@GeneratedValue too (if the id is not provided in the create call...) (hm how to write java annotations here so to avoid stackoverflow engine recoginizing it as user mention?) – m4gic Oct 07 '19 at 08:01
  • but the hostel field does – Sandro Rey Oct 07 '19 at 08:06
  • 1
    Yes, and that means that it will be checked whether the HTMDoc.hostel is null or not. But you have told nothing to the validation engine about the other field. – m4gic Oct 07 '19 at 08:08
  • Which @NotNull annotation are you using? Show the full package. – George Oct 10 '19 at 13:21

6 Answers6

8

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);
  }
}
George
  • 2,820
  • 4
  • 29
  • 56
1

Add the following annotation to the id and give it a try:

@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;

More info about @GeneratedValue annotation can be found in this great answer: Spring GeneratedValue annotation usage

xanmcgregor
  • 372
  • 2
  • 13
0

Put the annotation on the getter.

Maybe the validation is not supported in private fields.

AlexS
  • 5,295
  • 3
  • 38
  • 54
0
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
    @Id
    private Long id;
    @NotNull
    @JsonProperty("_location")
    private T location;

}

Using @Id on any property is mandatory in Model classes.

Raghunath
  • 29
  • 4
0

please add the following syntax on your id field

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Long id;

    @NotNull
    @JsonProperty("_location")
    private T location;

}

also use the validate annotation in your service class.

@Validated
@Service
public class DocService {
  public HTMDoc create(@Valid HTMDoc doc) {
    return repository.save(doc);
  }
}
TanvirChowdhury
  • 2,498
  • 23
  • 28
0

You need to use the @Validated annotation in your classes to inform the spring boot that these classes are should be validated by javax validations.

@Validated
@Component
public class Phone {
 //Your logic
}