title: Generic DAO, Spring, Hibernate
body: <p>I want to understand how can i implement the generic methods like add, edit, delete and search on my database, i have already made the connection (hibernate) and works fine</p>
  
  <p>I do have this method, that works</p>
  
  <p>Class: <em>GenericDAO</em></p>
  
  <pre><code>public &lt;T&gt; T save(final T o){
          Session session=HibernateUtil.getSessionFactory().openSession();
          Transaction trans=session.beginTransaction();
          Object object = (T) session.save(o);
          trans.commit();
          return (T) object;
      }
  </code></pre>
  
  <p>and in <em>Main</em></p>
  
  <pre><code>GenericDAO gen = new GenericDAO();
  gen.save(object); 
  </code></pre>
  
  <p>also i have others methods that i dont know how to use them</p>
  
  <p>Class: <em>GenericDAO</em></p>
  
  <pre><code>public void delete(final Object object){
     Session session=HibernateUtil.getSessionFactory().openSession();
     Transaction trans=session.beginTransaction();
     session.delete(object);
     trans.commit();
  }
  
  /***/
  public &lt;T&gt; T get(final Class&lt;T&gt; type, final int id){
      Session session=HibernateUtil.getSessionFactory().openSession();
      Transaction trans=session.beginTransaction();
      Object object = (T) session.get(type, id);
      trans.commit();
      return (T) object;
  }
  
  public &lt;T&gt; List&lt;T&gt; getAll(final Class&lt;T&gt; type) {
      Session session=HibernateUtil.getSessionFactory().openSession();
      Transaction trans=session.beginTransaction();
      final Criteria crit = session.createCriteria(type);
      List&lt;T&gt; list = crit.list();
      trans.commit();
      return list;
  }
  </code></pre>
  
  <p>Thank you</p>