title: Spring boot 1.3.8 + hibernate. Double initialization of ConstraintValidator on JPA side
body: <p>This question is an often one, but I came up to new levels of it(as it seems).
  Have been digging on this for a few hours.</p>
  
  <p>Have a spring boot (1.3.8 release) with hibernate. </p>
  
  <pre><code>&lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;java.version&gt;9&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.3.8.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
              &lt;version&gt;2.3.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Formed a ConstraintValidator with bean injections, which I've binded to an entity by type:
  @Target(ElementType.TYPE)</p>
  
  <p>On the controller side I've used @Valid annotation.</p>
  
  <p>And this is what I get(specially marked the 'isValid' to logs):
  Controller -> @Valid -> Logger tells the isValid is called (Validator's 'this' gives one object address)</p>
  
  <p>After this there is a call to save the entity:
  Controller -> Service -> JPA
  And from this point I see that the isValid is called again, bumping into the null reference of the autowired bean. The logger showed that the Validator object is different and shows a different memory block for the "this" output.</p>
  
  <p>When I removed the default constructor with a Constructor Injection one, I saw a more clearer picture:
  hibernate.newInstance failed to locate the default constructor.</p>
  
  <p>So the culprit seems to be the JPA, which is trying to create and validate the entity on save state, creating a separate instance of the validator.</p>
  
  <p>And this thing is related only when I'm doing custom validations on entities. Divided this into entity-dto, having the verifications on the DTO, and everything is working like a charm.</p>
  
  <p>Tried the suggestions from the net to define validators and other stuff, and nothing worked.</p>
  
  <p>Can someone suggest how to do this without DTO(hate doing duplications of entity-dto)?</p>
  
  <hr>
  
  <p>Here's an interesting thing to add.
  After a few tries managed to save the bean injections using static and constructor autowire(can't tell what was I doing wrong yesterday) and the isValid started to pass. But now I get a "null id" exception on the saveAndFlush, caused by the call to JPA's generic findByIpAndIdNot.</p>
  
  <p>I clearly getting more and more confused on this, because this is starting to loose sense. Before the call I get a verification that the DAO object is the same, Entity - same and etc, but at that point I catch a null pointer out of nowhere.</p>
  
  <p>Tried switching from JDK 9 to 8. Nothing.</p>