1

I found this implementation that basically do what I want.

@ConstraintComposition(OR)
@NotBlank
@Null
@ReportAsSingleViolation
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
public @interface NullOrNotBlank {
    String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

and I use it as

@field:NullOrNotBlank
@field:Size(max = 50)
var middleName: String? = null,

I've also tried

@field:NullOrNotBlank
@field:Size(max = 50)
@field:Column(nullable = true)
var middleName: String? = null,

But this composite constraints makes the field in the database (MySQL) not nullable. So what happens is that it's working fine until I save the Entity to the database.

holi-java
  • 29,655
  • 7
  • 72
  • 83
jasperagrante
  • 354
  • 4
  • 17
  • The generated column of `NullOrNotBlank` is `NOT NULL` which throws an error when I'm inserting an entity with null property – jasperagrante Jul 16 '17 at 14:29
  • hi, I think you try to using `hbm2ddl.auto` to update the constraint in an column. if the column exists, it doesn't update the constraints at all. so you should update it manually. please see here: https://stackoverflow.com/questions/15978368/hibernate-hbm2ddl-auto-update-doesnt-update-column-definitions-in-mysql/26681592 – holi-java Jul 16 '17 at 14:38
  • how about it now? if there is no problem. you should delete your question, since it is duplicated. – holi-java Jul 16 '17 at 14:42
  • I'm using hbm2ddl.auto=create-drop to recreate the tables at startup – jasperagrante Jul 16 '17 at 14:47
  • are you sure? have you using `@Column(nullable = true)` on your column field? the `@NullOrNotBlank` is only for validating and don't support for the column definition. – holi-java Jul 16 '17 at 14:58
  • Yes, I have already tried it. You can read my question. :) – jasperagrante Jul 16 '17 at 16:25

1 Answers1

0

After I embedded the hibernate-validator my tests also failed too.

Why did the nullable column is changed to non-nullable column by hibernate-validator?

the hibernate-validator will makes the column to non-nullable column, if you declared any non-nullable constraints on the column, for example:

@[ConstraintComposition(OR) NotBlank Null]
//                          ^---- it is a @NotNull constraint

@NotNull
public @interface NotBlank {...}

How to solve this problem?

IF you have seen my tests on github, you maybe found that I used a @Length which is a nullable constraint, then the hibernate-validator doesn't change the original column definition at all, for example:

@[ConstraintComposition(OR)  Length(min = 1)  Null]
//it is a nullable constraint ---^
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Thank you for your answer! What I did on my implementation is that I've replaced `@Length` with `@Size(min = 1)` so that it would be applicable to other data types. – jasperagrante Jul 17 '17 at 16:05
  • I've also noticed that my old implementation ignores the `@NotBlank` constraint. Because after I had changed it to `@Size(min = 1)` some test that asserts the number of violation failed. – jasperagrante Jul 17 '17 at 16:10
  • @jasperagrante Not at all. I found that `hibernate-validator` will change the `DDL` definition last night. – holi-java Jul 17 '17 at 16:11
  • I've also already implemented Flyway on my application so I would have control on the Schema and Migrations. Thanks again :) – jasperagrante Jul 17 '17 at 16:13