1 package org.objectweb.rentacar.persistance.dao; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.hibernate.ObjectNotFoundException; 9 import org.hibernate.Session; 10 import org.hibernate.Transaction; 11 import org.objectweb.rentacar.persistance.util.HibernateUtil; 12 13 public class GenericDAO { 14 15 public String create(Object o) throws DAOException { 16 Session session = null; 17 try{ 18 session = HibernateUtil.getSession(); 19 }catch (ExceptionInInitializerError e){ 20 throw new DAOException("Error in Hibernate Session instanciation", e); 21 }catch (Exception e){ 22 throw new DAOException("Error in Hibernate Session instanciation", e); 23 } 24 25 Transaction tx = null; 26 try{ 27 tx = session.beginTransaction(); 28 Serializable s = session.save(o); 29 tx.commit(); 30 return (String ) s; 31 }catch (Exception e) { 32 if (tx!=null) tx.rollback(); 33 if (o!=null){ 34 throw new DAOException("Error creating new "+o.getClass().getName(), e); 35 } 36 throw new DAOException("Error creating new Object of type null", e); 37 } 38 } 39 40 public String persist(Object o) throws DAOException { 41 Session session = null; 42 try{ 43 session = HibernateUtil.getSession(); 44 }catch (ExceptionInInitializerError e){ 45 throw new DAOException("Error in Hibernate Session instanciation", e); 46 }catch (Exception e){ 47 throw new DAOException("Error in Hibernate Session instanciation", e); 48 } 49 50 Transaction tx = null; 51 try{ 52 tx = session.beginTransaction(); 53 session.persist(o); 54 Serializable s = session.save(o); 55 56 tx.commit(); 57 return (String ) s; 58 }catch (Exception e) { 59 if (tx!=null) tx.rollback(); 60 if (o!=null){ 61 throw new DAOException("Error creating new "+o.getClass().getName(), e); 62 } 63 throw new DAOException("Error creating new Object of type null", e); 64 } 65 } 66 67 public void delete(Object o) throws DAOException { 68 Session session = null; 69 try{ 70 session = HibernateUtil.getSession(); 71 }catch (ExceptionInInitializerError e){ 72 throw new DAOException("Error in Hibernate Session instanciation", e); 73 }catch (Exception e){ 74 throw new DAOException("Error in Hibernate Session instanciation", e); 75 } 76 77 Transaction tx = null; 78 try{ 79 tx = session.beginTransaction(); 80 session.delete(o); 81 tx.commit(); 82 }catch (Exception e) { 83 if (tx!=null) tx.rollback(); 84 if (o!=null){ 85 throw new DAOException("Error deleting "+o.getClass().getName(), e); 86 } 87 throw new DAOException("Error deleting Object of type null", e); 88 } 89 } 90 91 public void update(Object o) throws DAOException { 92 Session session = null; 93 try{ 94 session = HibernateUtil.getSession(); 95 }catch (ExceptionInInitializerError e){ 96 throw new DAOException("Error in Hibernate Session instanciation", e); 97 }catch (Exception e){ 98 throw new DAOException("Error in Hibernate Session instanciation", e); 99 } 100 101 Transaction tx = null; 102 try{ 103 tx = session.beginTransaction(); 104 session.update(o); 105 tx.commit(); 106 }catch (Exception e) { 107 if (tx!=null) tx.rollback(); 108 if (o!=null){ 109 throw new DAOException("Error updating "+o.getClass().getName(), e); 110 } 111 throw new DAOException("Error updating Object of type null", e); 112 } 113 } 114 115 public Object retrieveById(Class clazz, Serializable s) throws DAOException { 116 Session session = null; 117 try{ 118 session = HibernateUtil.getSession(); 119 }catch (ExceptionInInitializerError e){ 120 throw new DAOException("Error in Hibernate Session instanciation", e); 121 }catch (Throwable e){ 122 throw new DAOException("Error in Hibernate Session instanciation", e); 123 } 124 125 Object result; 126 try { 127 result = session.load(clazz, s); 128 }catch (ObjectNotFoundException e){ 129 throw new DAOException("Error loading "+clazz.getName()+" with id : "+s, e); 130 } 131 catch (Throwable e){ 132 if (clazz !=null){ 133 throw new DAOException("Error loading "+clazz.getName()+" with id : "+s, e); 134 } 135 throw new DAOException("Error retrieving an Object of type null", e); 136 } 137 138 return result; 139 } 140 141 public List <Object > retrieveAll(Class clazz) throws DAOException { 142 Session session = null; 143 try{ 144 session = HibernateUtil.getSession(); 145 }catch (ExceptionInInitializerError e){ 146 throw new DAOException("Error in Hibernate Session instanciation", e); 147 } 148 catch (Exception e){ 149 throw new DAOException("Error in Hibernate Session instanciation", e); 150 } 151 try { 152 List list = session.createQuery("from "+clazz.getName()).list(); 153 List <Object > result = new ArrayList <Object >(); 154 for (Iterator iter = list.iterator();iter.hasNext();){ 155 result.add((Object ) iter.next()); 156 } 157 return result; 158 }catch (Exception e){ 159 if (clazz !=null){ 160 throw new DAOException("Error retrieving all"+clazz.getName(), e); 161 } 162 throw new DAOException("Error retrieving all Objects of type null", e); 163 } 164 } 166 167 } 168 | Popular Tags |