title: Hibernate/Junit Testing : Data still remain in session even after closing and opening it or cleaning it
body: <p>I am having quite a bit of trouble with the testing of my dao.
  I'll show the code at the end.</p>
  
  <p>The problem is:</p>
  
  <p>When i try to test different methods in dataDao: addData &amp; updateData, I want to start over with a clean slot. 
  Has if when i call update after add i don't want the data added in the test for addData.</p>
  
  <p>I do open a session in the setUp and close it in tearDown that are respecfully annoted with @Before and @After. The hibernate configuration is create-drop so that tables are new each time the session is closed and reopenned.</p>
  
  <p>I am a bit new to hibernate but I did my research and looked it up on the internet for quite a time/weeks.</p>
  
  <p>Here is what I am testing, the two classes: DataDAO and MyDaoManager (he is used because there are more then one table)</p>
  
  <p>Thank you in advance for your help,</p>
  
  <p>MyDaoManager.java</p>
  
  <pre><code>import java.util.List; 
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  
  public abstract class MyDaoManager&lt;T&gt; {
  
      private SessionFactory sessionFactory = null ;
  
      /**
       * &lt;h2&gt;Constructor which will get the sessionFactory from the class HibernateUtil&lt;/h2&gt;
       */
      public MyDaoManager(){
          this.sessionFactory = HibernateUtil.getSessionFactory() ;
      }
  
      /**
       * &lt;h2&gt;Method which gets the SessionFactory.&lt;/h2&gt;
       * @return A SessionFactory.
       */
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      /**
       * &lt;h2&gt;Method which sets the SessionFactory at the moment of the construction.&lt;/h2&gt;
       * @param sessionFactory The SessionFactory to set.
       */
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      /**
       * &lt;h2&gt;Method which adds a given object from the model in the database.&lt;/h2&gt;
       * @param object Object to add.
       */
      public boolean add(T object){
          Session session = getSessionFactory().getCurrentSession();
          Transaction transaction = session.beginTransaction();
  
          try {
              session.save(object);
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }
          return true;
      }
  
      /**
       * &lt;h2&gt;Method which updates a given Object from the model in the database.&lt;/h2&gt;
       * @param object Object to update.
       */
      public void update(T object){
          Session session = getSessionFactory().getCurrentSession();
          Transaction transaction = session.beginTransaction();
          try {
              session.merge(object);
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }
      }
  
      /**
       * &lt;h2&gt;Method which saves or updates a given Object from the model in the database.&lt;/h2&gt;
       * @param object Object to update.
       */
      public void saveOrUpdate(T object){
          Session session = getSessionFactory().getCurrentSession();
          Transaction transaction = session.beginTransaction();
          try {
              session.saveOrUpdate(object);
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }
      }
  
      /**
       * &lt;h2&gt;Method which gets an object from the model thanks to its id in the database.&lt;/h2&gt;
       * @param id The object's id to get.
       * @return An object.
       */
      public abstract T getOne(int id);
  
      /**
       * &lt;h2&gt;Method which gets all of an object from the model.&lt;/h2&gt;
       * @return A list of the object.
       */
      public abstract List&lt;T&gt; getAll();
  
      /**
       * &lt;h2&gt;Method which deletes a given object thanks to its id.&lt;/h2&gt;
       * @param id The object's id to delete.
       */
      public abstract void delete(int id);
  }
  </code></pre>
  
  <p>Here is dataDao.java:</p>
  
  <pre><code>import java.util.List;
  
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  
  public class DataDao extends MyDaoManager&lt;Data&gt;{
  
      @Override
      public Data getOne(int id) {
          Session session = getSessionFactory().getCurrentSession();
          Transaction  transaction = session.beginTransaction();
          Data data = null;
  
          try {
              Query query = session.createQuery("from Data d where d.id= :id").setParameter("id", id);
              data = (Data)query.uniqueResult();
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }   
          return data;
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public List&lt;Data&gt; getAll() {
          Session session = getSessionFactory().getCurrentSession();
          Transaction  transaction = session.beginTransaction();
          List&lt;Data&gt; list = null;
  
          try {
              list = session.createQuery("from Data d").list();
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }   
          return list;
      }
  
      @Override
      public void delete(int id) {
          Session session = getSessionFactory().getCurrentSession();
          Data data = getOne(id);
          Transaction transaction = session.beginTransaction();
          try {
              session.delete(data);
              transaction.commit();
          }
          catch (RuntimeException e){
              transaction.rollback();
              throw e ;
          }
      }
  }
  </code></pre>
  
  <p>Here is my test, DataDaoTest.java:</p>
  
  <pre><code>import static org.junit.Assert.assertTrue;
  import java.util.Date;
  import java.util.List;    
  import org.apache.commons.lang3.builder.EqualsBuilder;
  import org.hibernate.Session;
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  
  public class DataDaoTest{
  
      private Session session;
      private DataDao dataDao;
  
      @Before 
      public void setUp(){
          session = HibernateUtil.getSessionFactory().openSession();
          dataDao = new DataDao();
          dataDao.setSessionFactory(session.getSessionFactory());
      }
  
      @After
      public void tearDown(){
          session.close();
      }
  
      @Test
      public void addDataTestShouldWork(){
          Data dataTest = new Data();
          dataTest.setDate(new Date(System.currentTimeMillis()));
          dataTest.setSensor(new Sensor());
          dataTest.setValue("test");
          dataTest.setIsOnPhone(true);
  
          assertTrue("No add",dataDao.add(dataTest));
  
          List&lt;Data&gt; founded = dataDao.getAll();
          DataEqualsBuilder dataEqualsBuilder = new DataEqualsBuilder(dataTest);
          assertTrue("Not the same object added and founded", dataEqualsBuilder.equals(founded.get(0)));
      }
  
      @Test(expected=RuntimeException.class)
      public void addDataTestShouldNotWork(){
          dataDao.add(null);
      }
  
      @Test
      public void updateDataTestShouldWork(){
          Data dataTest = new Data();
          dataDao.add(dataTest);
          dataTest.setValue("test1");
          dataDao.update(dataTest);
  
          List&lt;Data&gt; founded = dataDao.getAll();
          DataEqualsBuilder dataEqualsBuilder = new DataEqualsBuilder(dataTest);
  
          assertTrue("Not the same object added and founded", dataEqualsBuilder.equals(founded.get(0)));
      }
  
  
      public class DataEqualsBuilder extends EqualsBuilder {
  
          private Data data;
  
          public DataEqualsBuilder( Data data){ this.data= data; }
  
          @Override
          public boolean equals(Object obj) {
              if (obj == null) { return false; }
              if (obj == this) { return true; }
              if (obj.getClass() != data.getClass()) {
                  return false;
              }
              Data rhs = (Data) obj;
              return (DataEqualsBuilder.reflectionEquals(data, rhs, "sensor"));
          }
      }
  }
  </code></pre>
  
  <p>Finally my hibernate configuration:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/APISensor&lt;/property&gt;
          &lt;!--  TODO create specific user --&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;root&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;false&lt;/property&gt;
  
          &lt;property name="hbm2ddl.auto"&gt;create-drop&lt;/property&gt;       
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>