I'm using Validator 3.1.0GA. Can a custom validator work with a child collection? I'm having problems with the child collection being empty when the validator on the parent entity runs during a .merge(). It is an empty PersistentBag.
I have an Entity with a custom validation on it, which in certain conditions checks that a child collection has exactly one item in it. I can't use @Size, because the condition depends on other properties in the entity.
What am I doing wrong, or is this even possible?
Thanks,
Jim
Code:
@ValidatorClass(FooChildCountValidator.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FooChildHasOneItem {
String message() default "The foo requires one, and only one, fooChild.";
}
Code:
public class FooChildCountValidator implements Serializable, Validator<FooChildHasOneItem>, PropertyConstraint {
public void initialize(FooChildHasOneItem parameters) {
}
public boolean isValid(Object o) {
Foo foo = (Foo) o;
if (someConditions() && foo.getFooChildren().size() != 0) {
return false;
}
return true;
}
}
Code:
@Entity
@FooChildHasOneItem
public class Foo implements Serializable {
...
@OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL}, targetEntity=FooChild.class)
@JoinColumn(name="fooID", nullable=false)
private Collection<FooChild> fooChildren = new ArrayList<FooChild>();
public Collection<FooChild> getFooChildren() {
return fooChildren;
}
public void setFooChildren(Collection<FooChild> fooChildren) {
this.fooChildren= fooChildren;
}
...
}