title: hibernate validator - custom factory validates twice
body: <p>Using a custom <code>ConstraintValidatorFactory</code> which is aware of the hibernate session by adding it to the configuration using following code results in double validation on any operation (create, update) this factory is responsible of.  </p>
  
  <p>Validator <code>ValidBeanDatabase</code> in this example is invoked twice with different instances.
  All other constraint's are validated once (<code>@ValidBean</code> as an example).</p>
  
  <p>The implementation of the factory and validator's is similar to the one in this post:<br>
  <a href="https://community.jboss.org/wiki/AccessingTheHibernateSessionWithinAConstraintValidator" rel="nofollow">https://community.jboss.org/wiki/AccessingTheHibernateSessionWithinAConstraintValidator</a></p>
  
  <p><strong>validation.xml</strong></p>
  
  <pre><code>&lt;constraint-validator-factory&gt;package.SessionConstraintValidatorFactory&lt;/constraint-validator-factory&gt;
  &lt;constraint-mapping&gt;package/Bean-validation.xml&lt;/constraint-mapping&gt;
  </code></pre>
  
  <p><strong>Java:</strong></p>
  
  <pre><code>final Configuration configuration = new Configuration().configure();
  
  // get ValidatorFactory from validation.xml
  final ValidatorFactory validatorFactory = Validation.byDefaultProvider().configure()
              .buildValidatorFactory();
  
  // ... add SessionFactory to custom ConstraintValidatorFactory
  ((SessionConstraintValidatorFactory)validatorFactory.getConstraintValidatorFactory()).setSessionFactory(sessionFactory);
  
  // set ValidatorFactory
  configuration.getProperties().put("javax.persistence.validation.factory", validatorFactory);
  </code></pre>
  
  <p><strong>Bean:</strong></p>
  
  <pre><code>@GroupSequence(value =
  {
      Bean.class, Create.class
  })
  @ValidBean // custom class-level constraint
  public class Bean
  {
      // properties and validator annotations
  }
  </code></pre>
  
  <p><strong>Bean-validation.xml</strong></p>
  
  <p>Adding Hibernate session aware constraint via xml mapping.</p>
  
  <pre><code>&lt;bean class="Bean" ignore-annotations="false"&gt;
      &lt;class&gt;
          &lt;constraint annotation="package.ValidBeanDatabase"&gt;
              &lt;groups&gt;
                  &lt;value&gt;package.groups.Create&lt;/value&gt;
              &lt;/groups&gt;
          &lt;/constraint&gt;
      &lt;/class&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Edit:</p>
  
  <p><strong>Hibernate-Configuration</strong></p>
  
  <pre><code>&lt;property name="javax.persistence.validation.group.pre-persist"&gt;
      javax.validation.groups.Default, package.groups.Create
  &lt;/property&gt;
  </code></pre>
  
  <p><em>Glassfish 3.1.2
  with Hibernate Validator 4.2</em></p>
  
  <p>@Gunner
  <em>Do you have any validation-related configuration in other places, e.g. persistence.xml?</em> <strong>No</strong></p>