Reputation: 12159
Validation using hibernate validator fails on the fields marked as @Transient
and @NotNull
.
However it fails only for those that were already stored in the DB, but now are traversed in the tree of objects. (as their value is null of course)
For the entities like this:
public class User {
@ManyToMany
@Fetch(org.hibernate.annotations.FetchMode.JOIN)
@Valid
private Set<Role> roles;
@NotNull
private String someField;
}
public class Role {
@ManyToMany
@Fetch(org.hibernate.annotations.FetchMode.JOIN)
@Valid
private Set<User> users;
}
As I do the validation of the User
and there are more users with particular Role
I have problem, that even if the current user validation succeeds, for the others I'm getting error.
I do this validation on the client side prio to submitting data to server side.
I've seen some ideas how to fix problems with validation of @Transient
and @NotNull
:
However these didn't cover my case (of having violations on related objects). And moreover I didn't come to validation problems on server side yet :). As to test that would be a next step.
So the question is, is there any way to explain validator, that transitive objects fetched from DB should be skipped during validation?
I was thinking of extending the JPATraversableResolver
that I currently use by overriding:
/**
* Determine if the Bean Validation provider is allowed to reach the property state
*
* @param traversableObject object hosting <code>traversableProperty</code> or null
* if validateValue is called
* @param traversableProperty the traversable property.
* @param rootBeanType type of the root object passed to the Validator.
* @param pathToTraversableObject path from the root object to
* <code>traversableObject</code>
* (using the path specification defined by Bean Validator).
* @param elementType either <code>FIELD</code> or <code>METHOD</code>.
*
* @return <code>true</code> if the Bean Validation provider is allowed to
* reach the property state, <code>false</code> otherwise.
*/
boolean isReachable(Object traversableObject,
Path.Node traversableProperty,
Class<?> rootBeanType,
Path pathToTraversableObject,
ElementType elementType);
however, I still face the problem of determining which properties to check and which not. If my check would be for transient fields of the currently persisted object, I'd not cover the case of persisting entities (2 related ones), where check would be on one of them and the other one would have problematic transient field.
Any ideas? Any experiences with that?
Upvotes: 1
Views: 2064
Reputation: 12159
OK, as no suggestion came, let me answer myself :)
I ended up with having special validation group on transient fields, and for this group I implemented separate TraversableResolver
that just checks, am I still directly in the validated entity?
This is done via check in: TraversableResolver.isReachable
where pathToTraversableObject
is checked for empty Strnig.
The reason for solution is that I was not able to validate related entities, as I was not able to decide if these need a validation or not (if they were modified or not). But the one that I'm submitting needs a validation for sure.
Upvotes: 1