title: Unable to generate constraint violation exception when testing Hibernate @Check constraints
body: <p>I am playing with Hibernate <code>@Check</code> annotation but <strong>can not have my test fail when constraints are not satisfied</strong>. Currently just using default Spring boot configuration with H2 database.</p>
  
  <p>What am I missing? Should there be a some kind of a <em>flush</em> after <code>save(..)</code>?</p>
  
  <p>When running test I see the table created correctly. If I copy the creation line from the log and using it create a table to my 'real' Postgres database I can test different inserts and see that this line is all fine with constraints.</p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>@Getter @Setter
  @Entity @Check(constraints = "a IS NOT NULL OR b IS NOT NULL")
  public class Constrained {
  
      @Id @GeneratedValue
      private Long id;
  
      private String a, b;
  }
  </code></pre>
  
  <p><strong>Test</strong></p>
  
  <pre><code>@DataJpaTest
  @RunWith(SpringRunner.class)
  public class HibernateCheckTest {
  
      @Resource // this repo is just some boiler plate code but attached at 
                // the bottom of question
      private ConstrainedRepository repo;
  
      @Test @Transactional // also tried without @Transactional
      public void test() {
          Constrained c = new Constrained();
          repo.save(c); // Am I wrong to expect some constraint exception here?
      }
  }
  </code></pre>
  
  <p><strong>Table generation script while running test</strong></p>
  
  <blockquote>
    <p>create table constrained (id bigint not null, a varchar(255), b
    varchar(255), primary key (id), check (a IS NOT NULL OR b IS NOT
    NULL))</p>
  </blockquote>
  
  <p><strong>Repository</strong> (not much to see in repo but just to show it):</p>
  
  <pre><code>public interface ConstrainedRepository
              extends CrudRepository&lt;Constrained, Long&gt; {
  }
  </code></pre>
  
  <p><strong>HOWEVER</strong></p>
  
  <p>If I use <code>EntityManager</code> so adding to my test class:</p>
  
  <pre><code>@PersistenceContext
  private EntityManager em;
  </code></pre>
  
  <p>and doing the persist like:</p>
  
  <pre><code>em.persist(c);
  em.flush();
  </code></pre>
  
  <p>instead of <code>repo.save(c)</code> I will get the exception.</p>
  
  <p><strong>AND</strong> </p>
  
  <p>studying the log from the original test with <code>repo.save(c)</code> more careful shows:</p>
  
  <blockquote>
    <p>org.springframework.test.context.transaction.TransactionContext:139 - Rolled back transaction for test:<br>
    ...<br>
    testException = [null],</p>
  </blockquote>
  
  <p>so for some reason this error is just wrapped and logged. How to have it "unwrapped" and thrown when using repository for persisting?</p>