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;
}
@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.
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.
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.
@NotNull validates that the annotated property value is not null. @AssertTrue validates that the annotated property value is true.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With