# Multiple validators for one class

**URL:** <https://discourse.hibernate.org/t/multiple-validators-for-one-class/1752>\
**Category:** Hibernate Validator\
**Created:** [November 20, 2018, 9:23am UTC](https://discourse.hibernate.org/t/multiple-validators-for-one-class/1752 "2018-11-20T09:23:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michal.hlavac](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/michal.hlavac/32/376_2.png) [@michal.hlavac](https://discourse.hibernate.org/u/michal.hlavac)\
**Post date:** [November 20, 2018, 9:23am UTC](https://discourse.hibernate.org/t/multiple-validators-for-one-class/1752/1 "2018-11-20T09:23:53Z")

</div>

Hi, I would like to create multiple validators for one class. Use case for this scenario is that sometimes I need validate object recursively (deep validation) with all references but sometimes I need shallow validation (validate only properties of class).

I prepared ConstraintMapping for both validations and do this:

```java
HibernateValidatorConfiguration cfg = Validation.byProvider(HibernateValidator.class).configure();

ConstraintMapping mapping1 = cfg.createConstraintMapping();
mapping1.type(Class.class). etc. // add deep mapping
cfg.addMapping(mapping1);
Validator validator1 = cfg.buildValidatorFactory().getValidator();

ConstraintMapping mapping2 = cfg.createConstraintMapping();
mapping2.type(Class.class). etc. // add shallow mapping
cfg.addMapping(mapping2);
Validator validator2 = cfg.buildValidatorFactory().getValidator();

```

But it ended with `javax.validation.ValidationException: HV000171: Class is configured more than once via the programmatic constraint declaration API`

I figured it out using multiple configurations:

```java
HibernateValidatorConfiguration cfg1 = Validation.byProvider(HibernateValidator.class).configure();
ConstraintMapping mapping1 = cfg1.createConstraintMapping();
mapping1.type(Class.class). etc. // add deep mapping
cfg1.addMapping(mapping1);
Validator validator1 = cfg1.buildValidatorFactory().getValidator();

HibernateValidatorConfiguration cfg2 = Validation.byProvider(HibernateValidator.class).configure();
ConstraintMapping mapping1 = cfg2.createConstraintMapping();
mapping2.type(Class.class). etc. // add shallow mapping
cfg2.addMapping(mapping2);
Validator validator1 = cfg2.buildValidatorFactory().getValidator();

```

It works, but I have doubts about correctness of this solution. Can I cache all validator instances for whole JVM to not always create them? Are there any recommendations about this usage?

Thank you very much, Miso

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [November 20, 2018, 6:16pm UTC](https://discourse.hibernate.org/t/multiple-validators-for-one-class/1752/3 "2018-11-20T18:16:58Z")

</div>

Hi Miso,

I confirm that, for now, you can’t have a condition on the cascading. It’s either enabled or not. We don’t have groups as we have for constraints.

Your solution seems fine and I confirm that the Validator instances are thread safe and we recommend to only instantiate them once (well, once for each in your case).
