0

While performing Spring MVC + Hibernate validation, the ValueObject is creating a problem. I am not allowed to use my Entity Objects directly in controller, so the ValueObject is created for each entity.

But now I need validation, in which I need to map @Valid annotation directly to Entity from my controller, which I am not allowed. So, I need to validate my Entity using ValueObject.

Here's my controller method:

@RequestMapping(value = "/saveOperatorDetails", method = RequestMethod.POST)
public String saveOperatorDetails(@ModelAttribute("operatorDetails") @Valid OperatorVO operatorVO,  BindingResult result) {

    if (result.hasErrors()) {
        return "admin.operator.registeroperator";
    }
}

Now, if I use my Entity, Operator my validation is a success.

But the ValueObjec(VO) can't validate the Hibernate Entity directly. Now what do I do to map ValueObject with Entity.

TheManish
  • 183
  • 1
  • 15

1 Answers1

0

So what I figured out was that, the validation @Valid was referring to my OperatorVO. So instead of validating my Entity I validated my ValueObject.

But now, I am validating my VO not my entity. Is it a good practice? I don't think so. If anybody comes with an answer please feel free to post one. Thank you!

TheManish
  • 183
  • 1
  • 15
  • I think it's the right thing to do. Why would it be a bad practice? – WilQu Aug 29 '14 at 11:31
  • Thanx, @WilQu so, what should I do? should I validate on both (VO & Entity), or just the VO validation is enough? – TheManish Aug 29 '14 at 11:41
  • 1
    I would say that the VO validation is enough, but you may want to check the integrity of the entity and validate it as well. It's up to you. – WilQu Aug 29 '14 at 11:49