Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why @AssertTrue doesn't work while @NotNull works?

I'm working on a entity library. I put some bean-validation annotations on my Entities.

I strongly believe a bean-validation implementation in on the class path. @javax.validation.constraints.NotNull works and @javax.validation.constraints.AssertTrue doesn't work.

class MyEntity {

    @AssertTrue // does't work
    public boolean hey() {
        return false;
    }

    @NotNull // works; violation while persist
    private String some;
}

What possibly did I do wrong with it?

I uses org.hibernate:hibernate-validator and changing it with org.apache.bval:bval-jsr doesn't make any difference.

UPDATE

The method is actually invoked. I check the log.

Here comes my method.

@AssertTrue(message = "a property must be eclusively system or owned")
private boolean execlusivelySystemOrOwned() {
    logger.info("execlusivelySystemOrOwnded()");
    final boolean result = system ^ (getOwner() != null);
    logger.log(Level.INFO, "result: {0}", result);
    return result;
}
like image 372
Jin Kwon Avatar asked Jul 25 '16 06:07

Jin Kwon


People also ask

What is the difference between @NotBlank and @NotNull?

@NotNull : The CharSequence, Collection, Map or Array object is not null, but can be empty. @NotEmpty : The CharSequence, Collection, Map or Array object is not null and size > 0. @NotBlank : The string is not null and the trimmed length is greater than zero.

What is the use of @NotNull annotation in spring boot?

One way we can protect our code is to add annotations such as @NotNull to our method parameters. By using @NotNull, we indicate that we must never call our method with a null if we want to avoid an exception.

Does NotBlank include NotNull?

So we might think that @NotBlank does allow null values, but it actually doesn't. The @NotNull class' isValid() method is called after the @NotBlank class' isValid(), hence forbidding null values.

What does @NotNull annotation Bean in Bean property?

@NotNull validates that the annotated property value is not null. @AssertTrue validates that the annotated property value is true.


1 Answers

I think I found the answer.

https://stackoverflow.com/a/12950573/330457

I had to rename the method to isExeclusivelySystemOrOwned.

That's why it's called Bean-Validation.

like image 61
Jin Kwon Avatar answered Sep 28 '22 03:09

Jin Kwon