Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Using @ConvertGroup And @GroupSequenceProvider together
PostPosted: Wed Jun 21, 2017 8:39 am 
Newbie

Joined: Wed Jun 21, 2017 8:30 am
Posts: 1
Hello I'm trying to solve this problem for quite some time now. I've created a dumbed down version to make te problem space a little clearer.

I have 2 DTO objects. A parent and a child, The child should only be validated if the parent has a certain state(createChild should be true). This state is checked by the GroupSequence provider and the approptiate validation groups are added. The ChildDto Field is annotated with @ConvertGroup and @Valid.

When createChild is true on the parentDto the group gets added but the @NotEmpty constraint is never checked. so the name of the child can still be empty.

Can anyone tell me how to make this work, or if there is another solution to this problem? Thanks in advance.

I've added my code below.

The ParentDto

Code:
@GroupSequenceProvider(ParentGroupSequenceProvider.class)
public class ParentDto{

    @Valid
    @ConvertGroup(from= CreateChild.class,to=Creation.class)
    private ChildDto childDto;

    private boolean createChild;

    public ChildDto getChildDto() {
        return childDto;
    }

    public void setChildDto(ChildDto childDto) {
        this.childDto = childDto;
    }

    public boolean isCreateChild() {
        return createChild;
    }

    public void setCreateChild(boolean createChild) {
        this.createChild = createChild;
    }
}



The Child Dto:
Code:
public class ChildDto {

    @NotEmpty(groups=Creation.class)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


The GroupSequenceProvider:
Code:
public class ParentGroupSequenceProvider implements DefaultGroupSequenceProvider<ParentDto> {

    static Logger log = Logger.getLogger(ParentGroupSequenceProvider.class.getName());
    @Override
    public List<Class<?>> getValidationGroups(ParentDto parentDto) {
        List<Class<?>> sequence = new ArrayList<Class<?>>();

      /*
       * ContactDataBis must be added to the returned list so that the validator gets to know
       * the default validation rules, at the very least.
       */


        if (parentDto == null){
            sequence.add(ParentDto.class);
            return sequence;
        }else {
      /*
       *  Here, we can implement a certain logic to determine what are the additional group of rules
       *  that must be applied.
       */
            if (parentDto.isCreateChild()) {
                sequence.add(CreateChild.class);
                log.info("Added CreateChild to groups");

            }
            sequence.add(ParentDto.class);
            return sequence;
        }
    }
}


Top
 Profile  
 
 Post subject: Re: Using @ConvertGroup And @GroupSequenceProvider together
PostPosted: Tue Jun 27, 2017 11:20 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 377
Can you pass the "createChild" state into the child DTO? Then you should be able to use the group sequence provider on the child DTO itself, taking that state into account.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.