0

I'm using Spring boot 1.5 with Hibernate 5.2, my problem is that the javax constraints annotations like @NotNull are not translated to the database.

@Entity
public class Date extends AbstractModel {

    @NotNull
        @NotEmpty
    protected String startDate;

    @NotNull
        @NotEmpty
    protected String endDate;

  // Getters and setters are omitted for brevity
}

This code gives no constraints on my MySQL database. I know I can use @column(nullable=false), but how comes that the javax annotations doesn't work?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
error
  • 926
  • 3
  • 10
  • 19
  • Java beans validations like NotNull, NotEmpty affects only objects fields. Validator checking fields and throw exceptions when requirements are not met. You can use these annotations anywhere not only for entity. – Adam Lesiak Apr 17 '18 at 14:03
  • I think that the Java beans validation affects the database too by creating the right constraints when creating the schema. Am I wrong ? – error Apr 17 '18 at 17:21
  • Oh, you are right. NotNull, NotEmpty and NotBlank should create not null constraints. When do you put these annotations? When table exists these annotations does not runs 'alter table(...)' – Adam Lesiak Apr 18 '18 at 08:00
  • I create the schema with hibernate, so the annotations are set from the beginning – error Apr 19 '18 at 08:39

0 Answers0