title: How to test org.hibernate.Session?
body: <p>Hi I have tested this code with junit 4:</p>
  
  <pre><code>public class UserDaoTest {
      /**
       * Runs before all test methods
       */
      @BeforeClass
      public static void createDB() {
          sessionFactory = HibernateUtil.getSessionFactory();
  
          userDao = new UserDaoImpl();
          languageDao = new LanguageDaoImpl();
          requestDao = new RequestDaoImpl();
          feedbackDao = new FeedbackDaoImpl();
          hostelDao = new HostelDaoImpl();
          imageDao = new ImageDaoImpl();
      }
  
      /**
       * Runs after all test methods
       */
      @AfterClass
      public static void closeSessionFactory() {
          HibernateUtil.shutdown();
      }
  
      /**
       * Runs before each test method
       * 
       * @return
       */
      @Before
      public void beginTransaction() {
          session = sessionFactory.getCurrentSession();
          transaction = session.beginTransaction();
      }
  
      /**
       * Runs after each test method
       */
      @After
      public void rollbackTransaction() {
          if (transaction != null &amp;&amp; transaction.isActive()) {    
              System.out.println("Rolling back trasnaction after @Test method");
              // rollback transaction so that tests don't modify database
              transaction.rollback();
          }
      }
  
      @Test
      public void testSaveUser() { 
  
          User user1 = new User("Ostap", "Stashyshyn", Gender.MALE);
  
          // save user. It delegates to `session.save(user1)` method.
          userDao.create(user1);
  
          Integer userId = user1.getUserId();
          Assert.assertNotNull(userId);
  
          // causes persistent entities to be saved into persistent context
          session.flush();
  
          // read languages from db
          List&lt;User&gt; users = userDao.readAll();
          Assert.assertThat(users.size(), CoreMatchers.is(1));
  
          // next method annotated with @After is running. 
          // It rollbacks transaction causing hibernate not to store data into database. Thus database state is the same as when entering this test method
      }
  </code></pre>
  
  <p>It tests successfully, but when I set breakpoint after <code>session.flush()</code> and go to db from command line I see that there is no row for user.</p>
  
  <p>Calling <code>userDao.readAll();</code> causes hibernate to issue select statement and return users?</p>
  
  <p>I see insert and select statements on console but nothing in db. But when I call <code>transaction.commit()</code> instead of <code>session.flush()</code> then appropriate data is in db. </p>
  
  <p>Is above code correct way to test saving user? Or I should call <code>transaction.commit()</code> instead of <code>session.flush()</code>?</p>
  
  <p><strong>EDIT</strong>: I shall adhere to this way of testing <code>org.hibernate.Session</code>?</p>
  
  <p>Thank you!</p>