title: Hibernate Error with Custom Bean Validation
body: <p>I'm trying to create a custom bean validation, so I write this custom constraint:</p>
  
  <pre><code>@Documented
  @Constraint(validatedBy = ValidPackageSizeValidator.class)
  @Target({ElementType.TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  public @interface ValidPackageSize {
  
    String message() default "{br.com.barracuda.constraints.ValidPackageSize}";
    Class&lt;?&gt;[] groups() default {};
    Class&lt;? extends Payload&gt;[] payload() default {};
  
  }
  </code></pre>
  
  <p>And a validator:</p>
  
  <pre><code>public class ValidPackageSizeValidator implements ConstraintValidator&lt;ValidPackageSize, PackageSize&gt; {
  
  ...
  
    @Override
    public boolean isValid(PackageSize value, ConstraintValidatorContext context) {
      ...validation login here..
    }
  
  }
  </code></pre>
  
  <p>Also, I wanted the validation to be performed on the service layer just after some decorators are called, so I created an another decorator to handle this task..</p>
  
  <pre><code>@Decorator
  public abstract class ConstraintsViolationHandlerDecorator&lt;T extends AbstractBaseEntity&gt; implements CrudService&lt;T&gt; {
  
    @Any
    @Inject
    @Delegate
    CrudService&lt;T&gt; delegate;
  
    @Inject
    Validator validator;
  
    @Override
    @Transactional
    public T save(T entity) {
        triggerValidations(entity);
        return delegate.save(entity);
    }
  
    private void triggerValidations(T entity) {
        List&lt;String&gt; errorMessages = validator.validate(entity).stream()
              .map(ConstraintViolation::getMessage)
              .collect(Collectors.toList());
        if (!errorMessages.isEmpty()) {
          throw new AppConstraintViolationException(errorMessages);
        }
    }
  }
  </code></pre>
  
  <p>Everything works, but if validations pass, hibernate throws an error:</p>
  
  <pre><code>ERROR [default task-6] (AssertionFailure.java:50) - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in br.com.barracuda.model.entities.impl.PackageSize entry (don't flush the Session after an exception occurs)
  </code></pre>
  
  <p>My entities use auto-generated <code>id</code> values.</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  protected Long id;
  </code></pre>
  
  <p>Using Widlfly 9 with JEE 7.</p>