title: Hibernate: transactional insert and unit testing
body: <p>I'm writing a unit test for a DAO call to insert a record into the DB using Hibernate.</p>
  
  <p>It's transactional and I used @Transactional in that way:</p>
  
  <pre><code>@RunWith( SpringJUnit4ClassRunner.class )
  @ContextConfiguration(locations={"/spring-servlet.xml"})
  @Transactional
  public class MyDaoTest {
  
      @Autowired
      private MyDao dao;
  
      @Test
      @Rollback(false)
      public void testAddEmployee() {
  
  
          // Create some data (line 1)
  
          dao.addPerson(newPerson); // (line 2)
  
          // Query DB (line 3)
  
          // Check result (line 4)
      } // (line 5)
  }
  </code></pre>
  
  <p>In the above code, the operation is committed only when the function terminates at line 5.</p>
  
  <p>The fact is that at line 3 and 4 I need to query the DB to check if the instert has been completed succesfully. But I can't do that because my data is still not persisted.</p>
  
  <p>What can I do in order to test this insert? Is there a way to commit before the end of the function?</p>
  
  <p>I'm adding some more info here. This is the console output:</p>
  
  <pre><code>Hibernate: select person0_.ID as ID1_, person0_.NAME as NAME1_ from PERSON person0_
  Hibernate: insert into PERSON (NAME) values (?, ?)
  Hibernate: select person0_.ID as ID1_, person0_.NAME as NAME1_ from PERSON person0_
  </code></pre>
  
  <p>For testing I'm using JUnit</p>