-->
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.  [ 2 posts ] 
Author Message
 Post subject: Elegant way to exclude some fields from validation on demand
PostPosted: Fri Jan 13, 2012 8:11 pm 
Newbie

Joined: Sun Mar 12, 2006 2:44 pm
Posts: 10
Hi,

I have hierarchy of classes (JPA Entities) on which my business service does two levels of validation. At the beginning preliminary validation is performed to check if the fields required for computation are provided. Later some business computation occurs which fills some other fields and at the end (on Hibernate save/update) default/full validation is performed (all provided by user and computed fields are required).

Usually I specified that some fields are required only on validation with given validation group, which required only to add group=MyGroup to @NotNull or other annotation. Here I would like to validate all fields by default and skip selected fields on validation with given group. I was able to achieve that with defining @NotNull(groups = {Default.class} for fields that should be skipped on preliminary validation and @NotNull(groups = {Defaut.class, PreComputationGroup.class} for the other fields validated in any case.

Unfortunately it's a class hierarchy with many fields and it doesn't look good, is not intuitive and error prone on modifications by other people. I tried with redefining groups (@GroupSequence), groups hierarchy and some other cases, but without success. Maybe you could suggest some better, more generic solution to my case?


Minimal code sample for better visualization:
Code:
interface PreComputationGroup {}

class MyEntity {
    @NotNull(groups = {Default.class, PreComputationGroup.class}
    private String fieldProvidedByUserRequiredOnComputationAndOnPersist;

    @NotNull(groups = Default.class)
    private String calculatedFieldRequiredOnlyOnPersist;  //should be skipped at PreComputation validation
    ...
}

class MyService {
    void doBusiness(MyEntity myEntity) {
        validator.validate(myEntity, PreComputationGroup.class);
        doComputationAndFillAdditionalFields(myEntity);
        session.save(myEntity);   //full/default validation is performed
    }
}


Top
 Profile  
 
 Post subject: Re: Elegant way to exclude some fields from validation on demand
PostPosted: Mon Jan 16, 2012 4:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

you are already doing the right thing. Using groups for this type of requirement is exactly the idea in the Bean Validation specification. You don't need to explicitly specify the default group for calculatedFieldRequiredOnlyOnPersist, but maybe it helps to make things more clear.

Out of interest, do you have a suggestion on how this could be done easier? What would you expect?

--Hardy


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

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.