title: Spring Form Validation (JSR-303)
body: <p>Getting an error while using validation api. Am validating the input in register.jsp
  i have included the required jars.
  validation-api-1.1.0.Final.jar
  hibernate-validator-5.0.1.Final.jar</p>
  
  <p>register.jsp</p>
  
  <pre><code>&lt;h4&gt;Register&lt;/h4&gt;
  &lt;form:form method="post" action="register" modelAttribute="user"&gt;
      &lt;form:label path="userName"&gt;UserName&lt;/form:label&gt;
      &lt;form:input path="userName"/&gt;
      &lt;form:errors path="userName"/&gt;&lt;br&gt;
      &lt;form:label path="password"&gt;Password&lt;/form:label&gt;
      &lt;form:password path="password"/&gt; &lt;br&gt;
      &lt;input type="submit" value="Register"&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>@Entity
  @Table(name="usertable")
  public class User {
      @Id
      @Column(name="username")
      @Size(min=6,max=10,message="Error Message")
      private String userName;
      @Column(name="password")
      private String password;
  </code></pre>
  
  <p>When i enter username less than 6,result.hasErrors() is false , but it should be true.
  RegisterController.java</p>
  
  <pre><code>@Controller
  @RequestMapping(value="register")
  public class RegisterController {
      @Autowired
      private RegisterService registerService;
      @RequestMapping(method=RequestMethod.POST)
      public ModelAndView register(@Valid User user,BindingResult result){
          System.out.println("hi");
          if(result.hasErrors()){
              return new ModelAndView("index");
          }
          else{
              if(registerService.register(user))  
                  return new ModelAndView("success");
              return new ModelAndView("failure");
          }
      }
      public RegisterService getRegisterService() {
          return registerService;
      }
      public void setRegisterService(RegisterService registerService) {
          this.registerService = registerService;
      }
  }
  </code></pre>
  
  <p>RegisterService.java</p>
  
  <pre><code>@Service
  public class RegisterService {
      @Autowired
      private RegisterDao registerDao;
      @Transactional
      public boolean register(User user){
          registerDao.register(user);
          return true;
      }
      public RegisterDao getRegisterDao() {
          return registerDao;
      }
      public void setRegisterDao(RegisterDao registerDao) {
          this.registerDao = registerDao;
      }
  }
  </code></pre>
  
  <p>RegisterDao.java</p>
  
  <pre><code>@Repository
  public class RegisterDao {
      @Autowired
      private SessionFactory sessionFactory;
      public boolean register(User user){
          this.sessionFactory.getCurrentSession().save(user);
          return true;
      }
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  }
  </code></pre>
  
  <p>Am getting this error</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [spring] in context with path [/Annotated] threw exception [Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.labs.domain.User] during persist time for groups [javax.validation.groups.Default, ]
  List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='Error Message', propertyPath=userName, rootBeanClass=class com.labs.domain.User, messageTemplate='Error Message'}
  ]] with root cause
  javax.validation.ConstraintViolationException: Validation failed for classes [com.labs.domain.User] during persist time for groups [javax.validation.groups.Default, ]
  List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='Error Message', propertyPath=userName, rootBeanClass=class com.labs.domain.User, messageTemplate='Error Message'}
  ]
  </code></pre>
  
  <p>Can someone tell me where am going wrong ?</p>