-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Validation on child collection
PostPosted: Fri Jun 26, 2009 4:17 pm 
Newbie

Joined: Wed Jan 21, 2009 5:55 pm
Posts: 4
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;
    }
...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.