Guest User

Untitled

a guest
Nov 16th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. package pl.polsl.aei.SystemPraktyk.server;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import pl.polsl.aei.SystemPraktyk.model.utils.HibernateUtil;
  7.  
  8. public class HibernateHelper {
  9.  
  10.     @SuppressWarnings({ "rawtypes", "unchecked" })
  11.     public static <A,B> List<B> query(A classDB, B classDTO) {
  12.        
  13.         org.hibernate.Session s = null;
  14.         List<B> listDBO = new ArrayList<B>();
  15.        
  16.         try {
  17.             s = HibernateUtil.getSessionFactory()
  18.                     .openSession();
  19.            
  20.             List items = s.createCriteria(classDB.getClass()).list();
  21.             for(Object o : items)
  22.             {
  23.                 if(o.getClass().isAssignableFrom(classDB.getClass()))
  24.                 {
  25.                     listDBO.add((B) classDTO.getClass().getDeclaredConstructor(classDB.getClass()).newInstance(o));
  26.                 }
  27.             }
  28.  
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.         finally
  33.         {
  34.             if(s != null)
  35.                 s.close();
  36.         }
  37.         return listDBO;
  38.     }
  39. }
Add Comment
Please, Sign In to add comment