title: Expecting database exceptions with JUnit, Spring and HIbernate
body: <p>Im using spring 3.0.7, JUnit 4.11 and DBUnit 2.4.9.
  I am trying to test trowing exception when saving entity with many to one relationship not specified (set to null). Here is the entity:</p>
  
  <pre><code>&lt;class name="DistributionPoint" table="distribution_point"&gt;
          &lt;id name="id" unsaved-value="null" type="java.lang.Long"&gt;
              &lt;column name="id"&gt;&lt;/column&gt;
              &lt;generator class="hilo"&gt;&lt;/generator&gt;
          &lt;/id&gt;
  
          &lt;property name="name" type="java.lang.String"&gt;
              &lt;column name="name" length="255"&gt;&lt;/column&gt;
          &lt;/property&gt;
          &lt;property name="description" type="java.lang.String"&gt;
              &lt;column name="description" length="1024"&gt;&lt;/column&gt;
          &lt;/property&gt;
  
          &lt;property name="image" type="binary"&gt;
              &lt;column name="image"&gt;&lt;/column&gt;
          &lt;/property&gt;
  
          &lt;many-to-one name="site" class="Site"&gt;
              &lt;column name="siteId" not-null="true"&gt;&lt;/column&gt;
          &lt;/many-to-one&gt;
  
      &lt;/class&gt;
  </code></pre>
  
  <p>I've created the following test:</p>
  
  <pre><code>@TransactionConfiguration(defaultRollback=false, transactionManager="transactionManager")
  //@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:/spring/mysql-test-context.xml" })
  public class MysqlDistributionPointTest extends AbstractDatabaseTest {
  </code></pre>
  
  <p>AbstractDatabaseTest extends AbstractTransactionalJUnit4SpringContextTests.</p>
  
  <p>The test method is:</p>
  
  <pre><code>@Test(expected=org.hibernate.PropertyValueException.class)
      public void testSaveWithoutSite() {
          // set up DP
          DistributionPoint distributionPoint = new DistributionPoint();
          distributionPoint.setName( "Test distribution point" );
          distributionPoint.setDescription( "Test description" );
  
          // save DP
          this.distributionPointDao.save( distributionPoint );
      }
  </code></pre>
  
  <p>but when I run the test I get</p>
  
  <pre><code>org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:648)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:515)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)
      at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
      at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:155)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
      ... 27 more
  </code></pre>
  
  <p>When I change expected exception to DataIntegrityViolationException like this</p>
  
  <pre><code>@Test(expected = DataIntegrityViolationException.class)
      public void testSaveWithoutSite() {
  DistributionPoint distributionPoint = new DistributionPoint();
          distributionPoint.setName( "Test distribution point" );
          distributionPoint.setDescription( "Test description" );
  
          this.distributionPointDao.save( distributionPoint );
      }
  </code></pre>
  
  <p>I get the following stack trace</p>
  
  <pre><code>java.lang.Exception: Unexpected exception, expected&lt;org.springframework.dao.DataIntegrityViolationException&gt; but was&lt;org.hibernate.PropertyValueException&gt;
      at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
      at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665)
      at com.hyperoptic.infinity.commons.dao.HbmGenericDaoBase.save(HbmGenericDaoBase.java:47)
      at data.test.MysqlDistributionPointTest.saveWithoutSite(MysqlDistributionPointTest.java:95)
      at data.test.MysqlDistributionPointTest.testSaveWithoutSite(MysqlDistributionPointTest.java:82)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
      ... 24 more
  
  org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:648)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:515)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)
      at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)
      at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
      at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:155)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
      ... 27 more
  </code></pre>
  
  <p>I get this exception even if I surround save call like this:</p>
  
  <pre><code>@Test
      public void testSaveWithoutSite() {
          DistributionPoint distributionPoint = new DistributionPoint();
          distributionPoint.setName( "Test distribution point" );
          distributionPoint.setDescription( "Test description" );
  
          try {
              // save DP
              this.distributionPointDao.save( distributionPoint );
          } catch (Exception e) {
              // TODO: handle exception
          }
      }
  </code></pre>
  
  <p>which really confuses me.. anybody had a similar problem?</p>