title: Null pointer exception Custom Validation in spring + hibernate
body: <p><strong>My Custom Validation Classes</strong><br><br>
  1.UserAvailablity.java</p>
  <pre><code>
      package com.vicky.finalYearProject.customValidations;
          
          import java.lang.annotation.ElementType;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          
          import javax.validation.Constraint;
          import javax.validation.Payload;
          
          @Constraint(validatedBy = UserAvailabilityValidator.class)
          @Target({ElementType.METHOD, ElementType.FIELD})
          @Retention(RetentionPolicy.RUNTIME)
          public @interface UserAvailability {
              
              public String message() default &quot;Email is already Registered&quot;;
              
              public Class&lt;?&gt;[] groups() default { };
              
              public Class&lt;? extends Payload&gt;[] payload() default { };
              
          }
  </code></pre>
  <p><br>2.UserAvailabilityValidator.class<br><br></p>
  <pre><code>
      package com.vicky.finalYearProject.customValidations;
          
          import javax.validation.ConstraintValidator;
          import javax.validation.ConstraintValidatorContext;
          
          import org.springframework.beans.factory.annotation.Autowired;
          
          import com.vicky.finalYearProject.Service.CheckUserAvailabilityService;
          
          public class UserAvailabilityValidator implements ConstraintValidator&lt;UserAvailability, String&gt;{
          
              @Autowired
              CheckUserAvailabilityService availabilityService;
              
              @Override
              public void initialize(UserAvailability constraintAnnotation) {
                  ConstraintValidator.super.initialize(constraintAnnotation);
                  
              }
              
              @Override
              public boolean isValid(String email, ConstraintValidatorContext context) {
                  
                  boolean available = false;
          
                  available = availabilityService.isAvailable(email);//return true or false
                          
                  return available;
              }
          
          }
  </code></pre>
  <p><br><strong>My Entities</strong><br><br></p>
  <ol>
  <li>User.java</li>
  </ol>
  <pre><code>
      package com.vicky.finalYearProject.Entity;
          
          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.Table;
          import javax.persistence.UniqueConstraint;
          import javax.validation.Valid;
          import javax.validation.constraints.NotNull;
          import javax.validation.constraints.Pattern;
          
          import org.hibernate.validator.constraints.Length;
          import org.springframework.stereotype.Component;
          
          import com.vicky.finalYearProject.customValidations.UserAvailability;
          
          @Entity
          @Component
          @Table( name = &quot;customers&quot;,
                      uniqueConstraints=
                      @UniqueConstraint(columnNames={&quot;email&quot;})
                  )
          public class User {
              
              @Id
              @Column(name = &quot;id&quot;)
              @GeneratedValue(strategy = GenerationType.IDENTITY)
              private int id;
              
              @NotNull
              @Column(name = &quot;email&quot;, length = 50)
              @Pattern(regexp = &quot;^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$&quot;)
              @UserAvailability//custom validator
              private String email;
              
              @NotNull
              @Column(name = &quot;password&quot;)
              @Length(min = 1 , message = &quot;Enter Valid Password&quot;)
              private String password;
              
              @NotNull
              @Column(name = &quot;firstName&quot;)
              @Length(min = 1 , message = &quot;Enter Valid firstName&quot;)
              private String firstName;
              
              @NotNull
              @Column(name = &quot;middleName&quot;)
              @Length(min = 1 , message = &quot;Enter Valid middleName&quot;)
              private String middleName;
              
              @NotNull
              @Column(name = &quot;lastName&quot;)
              @Length(min = 1 , message = &quot;Enter Valid lastName&quot;)
              private String lastName;
              
              public int getId() {
                  return id;
              }
          
              public void setId(int id) {
                  this.id = id;
              }
          
              public String getEmail() {
                  return email;
              }
          
              public void setEmail(String email) {
                  this.email = email;
              }
          
              public String getPassword() {
                  return password;
              }
          
              public void setPassword(String password) {
                  this.password = password;
              }
          
              public String getFirstName() {
                  return firstName;
              }
          
              public void setFirstName(String firstName) {
                  this.firstName = firstName;
              }
          
              public String getMiddleName() {
                  return middleName;
              }
          
              public void setMiddleName(String middleName) {
                  this.middleName = middleName;
              }
          
              public String getLastName() {
                  return lastName;
              }
          
              public void setLastName(String lastName) {
                  this.lastName = lastName;
              }
          
              @Override
              public String toString() {
                  return &quot;User [id=&quot; + id + &quot;, email=&quot; + email + &quot;, password=&quot; + password + &quot;, firstName=&quot; + firstName
                          + &quot;, middleName=&quot; + middleName + &quot;, lastName=&quot; + lastName + &quot;]&quot;;
              }
          }
  </code></pre>
  <p><br><strong>My xml form Mapping with User class... by form:form</strong><br><br></p>
  <p>*</p>
  <pre><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
      pageEncoding=&quot;ISO-8859-1&quot;%&gt;
  &lt;%@ taglib prefix= &quot;form&quot; uri =&quot;http://www.springframework.org/tags/form&quot; %&gt;
  
  &lt;body bgcolor = &quot;black&quot;&gt;
      &lt;h1 style=&quot;color: white&quot;&gt;ADD USER INFORMATION&lt;/h1&gt;
      
      &lt;hr&gt;
      
      &lt;div style=&quot;color: white&quot;&gt;
      
          &lt;form:form  action =&quot;${pageContext.request.contextPath}/user/conformation&quot; 
                      modelAttribute=&quot;user&quot; 
                      method=&quot;post&quot;&gt;
                      
                      First Name : &lt;form:input path=&quot;firstName&quot;/&gt;
                      &lt;form:errors path =&quot;firstName&quot; cssClass=&quot;error&quot;/&gt;
                      &lt;br&gt;&lt;br&gt;
                      
                      Middle Name: &lt;form:input path=&quot;middleName&quot;/&gt;
                      &lt;form:errors path =&quot;middleName&quot; cssClass=&quot;error&quot;/&gt;
                      &lt;br&gt;&lt;br&gt;
                      
                      Last Name: &lt;form:input path=&quot;lastName&quot;/&gt;
                      &lt;form:errors path =&quot;lastName&quot; cssClass=&quot;error&quot;/&gt;
                      &lt;br&gt;&lt;br&gt;
                      
                      Email : &lt;form:input path=&quot;email&quot;/&gt;
                      &lt;form:errors path =&quot;email&quot; cssClass=&quot;error&quot;/&gt;
                      &lt;br&gt;&lt;br&gt;
                      
                      password : &lt;form:input type=&quot;password&quot; path=&quot;password&quot;/&gt;
                      &lt;form:errors path =&quot;password&quot; cssClass=&quot;error&quot;/&gt;
                      &lt;br&gt;&lt;br&gt;                    
                      
                      &lt;input type = &quot;Submit&quot; value=&quot;next&quot;/&gt;
                      
          &lt;/form:form&gt;
          
      &lt;/div&gt;
      
      &lt;hr&gt;
      
      &lt;a href=&quot;${pageContext.request.contextPath}/&quot; style=&quot;color: white&quot;&gt; cancle &lt;/a&gt;
      
  &lt;/body&gt;
  &lt;/html&gt;
  
  *
  </code></pre>
  <p><strong>ERROR I GOT when I submit My jsp page</strong></p>
  <pre><code>
      Aug 11, 2020 12:29:33 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
          INFO: HHH000397: Using ASTQueryTranslatorFactory
          Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.firstName as firstNam3_0_, user0_.lastName as lastName4_0_, user0_.middleName as middleNa5_0_, user0_.password as password6_0_ from customers user0_ where user0_.email='sdghag@jsjdhak.zdhzg'
          Aug 11, 2020 12:29:40 PM org.apache.catalina.core.StandardWrapperValve invoke
          SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/finalYearProject2] threw exception [Request processing failed; nested exception is javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.] with root cause
          java.lang.NullPointerException
              at com.vicky.finalYearProject.customValidations.UserAvailabilityValidator.isValid(UserAvailabilityValidator.java:26)
              at com.vicky.finalYearProject.customValidations.UserAvailabilityValidator.isValid(UserAvailabilityValidator.java:1)
              at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:180)
              at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:62)
              at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:75)
              at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:130)
              at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:123)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:555)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:518)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:488)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:450)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:400)
              at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:172)
              at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:116)
              at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:80)
              at org.hibernate.action.internal.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:197)
              at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:75)
              at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645)
              at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282)
              at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263)
              at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317)
              at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332)
              at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
              at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:196)
              at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:127)
              at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
              at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
              at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
              at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
              at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
              at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:713)
              at org.hibernate.internal.SessionImpl.save(SessionImpl.java:705)
              at org.hibernate.internal.SessionImpl.save(SessionImpl.java:700)
              at com.vicky.finalYearProject.DAO.UserDaoImplimantation.addUser(UserDaoImplimantation.java:16)
              at com.vicky.finalYearProject.Service.PurchaseService.purchase(PurchaseService.java:46)
              at com.vicky.finalYearProject.Controller.UserController.purchaseconformed(UserController.java:74)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.base/java.lang.reflect.Method.invoke(Method.java:567)
              at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
              at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
              at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
              at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
              at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
              at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
              at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
              at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
              at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
              at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
              at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
              at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
              at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
              at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:117)
              at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)
              at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)
              at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
              at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
              at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
              at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
              at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
              at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
              at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
              at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
              at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
              at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
              at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
              at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
              at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
              at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
              at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
              at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
              at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
              at java.base/java.lang.Thread.run(Thread.java:835)
  
  </code></pre>
  <p><strong>Hibernaete.cfg.xml</strong></p>
  <pre><code>
      &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
      &lt;!DOCTYPE hibernate-configuration PUBLIC
              &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
              &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
      &lt;hibernate-configuration&gt;
          &lt;session-factory&gt;
                &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
                &lt;property name=&quot;hibernate.connection.password&quot;&gt;120699@mysqL&lt;/property&gt;
                &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost:3306/final_year_project&lt;/property&gt;
                &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
                &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
                &lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;/property&gt;
                &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          &lt;/session-factory&gt;
      &lt;/hibernate-configuration&gt;
  
  </code></pre>
  <p><strong>Spring Configuration</strong><br><br></p>
  <p>1.ViewResolverConfigrationClass.java</p>
  <pre><code>
      package com.vicky.finalYearProject.Configuration;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.ViewResolver;
      import org.springframework.web.servlet.config.annotation.EnableWebMvc;
      import org.springframework.web.servlet.view.InternalResourceViewResolver;
      
      @Configuration
      @EnableWebMvc
      @ComponentScan(basePackages = &quot;com.vicky.finalYearProject&quot;)
      public class ViewResolverConfigrationClass {
      
          @Bean
          public ViewResolver viewResolver() {
              
              InternalResourceViewResolver viewresolver = new InternalResourceViewResolver();
              
              viewresolver.setPrefix(&quot;/WEB-INF/view/&quot;);
              
              viewresolver.setSuffix(&quot;.jsp&quot;);
              
              return viewresolver;
          }
      }
  
  </code></pre>
  <p><br>2.DispecherServletConfigClass.java<br><br></p>
  <pre><code>
      package com.vicky.finalYearProject.Configuration;
      
      import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
      
      public class DispecherServletConfigClass extends AbstractAnnotationConfigDispatcherServletInitializer {
      
          @Override
          protected Class&lt;?&gt;[] getRootConfigClasses() {
              return null;
          }
      
          @Override
          protected Class&lt;?&gt;[] getServletConfigClasses() {
              return new Class[] {ViewResolverConfigrationClass.class};
          }
      
          @Override
          protected String[] getServletMappings() {
              return new String[] {&quot;/&quot;};
          }
      }
  
  </code></pre>
  <p>I tried alot to figure out Even it works well without <strong>@UserAvailability</strong> in User.java Class<br>
  <strong>EVEN it works well with @UserAvailability when UserAvailabilityValidator.IsValid() returns false</strong><br>
  Can Anyone please solve this issue For me...Thank you`enter code here<br><br>
  <strong>I THINK HIBERNATE IS AGAIN VALIDATING ALL CONSTRAINT AFTER SPRING THIS ERROR ERROR IS COME WHEN I TRY TO USE SAVE METHOD IN HIBERNATE</strong></p>
  <p><strong>UserController.java</strong></p>
  <pre><code>
      package com.vicky.finalYearProject.Controller;
      
      import java.util.List;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.validation.Valid;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.validation.BindingResult;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.PostMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      import com.vicky.finalYearProject.Entity.Department;
      import com.vicky.finalYearProject.Entity.Student;
      import com.vicky.finalYearProject.Entity.University;
      import com.vicky.finalYearProject.Service.StudentSignupService;
      
      @Controller
      @RequestMapping(&quot;/student&quot;)
      public class StudentController {
      
          @Autowired
          Student student;
          
          @Autowired
          StudentSignupService studentSignupSvc;
          
          List&lt;String&gt; universities;
          List&lt;String&gt; departments;
          
          @GetMapping(&quot;/signup&quot;)
          public String studentLogin(Model model) {
              
              universities = studentSignupSvc.getAllUniversity();
              departments = studentSignupSvc.getAllDepartment();
              
              model.addAttribute(&quot;student&quot;, student);
              model.addAttribute(&quot;universityOptions&quot;, universities);
              model.addAttribute(&quot;departmentOptions&quot;, departments);
              
              return &quot;student/signup&quot;;
          }
          
          @PostMapping(&quot;/conformation&quot;) 
          public String studentConformation(
                              @Valid @ModelAttribute(&quot;student&quot;) Student student,
                              BindingResult result,
                              Model model,
                              HttpServletRequest request) {
              
              if(result.hasErrors()) {
                  model.addAttribute(&quot;universityOptions&quot;, universities);
                  model.addAttribute(&quot;departmentOptions&quot;, departments);
                  return &quot;student/signup&quot;;
              }
              else {
                  
                  String universityName = (String)request.getParameter(&quot;theUniversity&quot;);
                  String departmentName = (String)request.getParameter(&quot;theDepartment&quot;);
                  
                  
                  University university = studentSignupSvc.getUniversity(universityName);
                  Department department = studentSignupSvc.getDepartment(departmentName);
                  
                  
                  student.setUniversity(university);
                  student.setDepartment(department);
      
                  studentSignupSvc.signup(student);  //CAUSE OF ERROR If I comment this line everything work fine
  //and sigup service is perfactly auto wired i checked it.
                  
                  return &quot;student/studentConformation&quot;;
              }       
          }
      }
  
  </code></pre>