KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > persistance > dao > GenericDAO


1 package org.objectweb.rentacar.persistance.dao;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
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 JavaDoc create(Object JavaDoc o) throws DAOException {
16         Session session = null;
17         try{
18             session = HibernateUtil.getSession();
19         }catch (ExceptionInInitializerError JavaDoc e){
20             throw new DAOException("Error in Hibernate Session instanciation", e);
21         }catch (Exception JavaDoc 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 JavaDoc s = session.save(o);
29             tx.commit();
30             return (String JavaDoc) s;
31         }catch (Exception JavaDoc 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 JavaDoc persist(Object JavaDoc o) throws DAOException {
41         Session session = null;
42         try{
43             session = HibernateUtil.getSession();
44         }catch (ExceptionInInitializerError JavaDoc e){
45             throw new DAOException("Error in Hibernate Session instanciation", e);
46         }catch (Exception JavaDoc 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 JavaDoc s = session.save(o);
55             
56             tx.commit();
57             return (String JavaDoc) s;
58         }catch (Exception JavaDoc 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 JavaDoc o) throws DAOException {
68         Session session = null;
69         try{
70             session = HibernateUtil.getSession();
71         }catch (ExceptionInInitializerError JavaDoc e){
72             throw new DAOException("Error in Hibernate Session instanciation", e);
73         }catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc o) throws DAOException {
92         Session session = null;
93         try{
94             session = HibernateUtil.getSession();
95         }catch (ExceptionInInitializerError JavaDoc e){
96             throw new DAOException("Error in Hibernate Session instanciation", e);
97         }catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc retrieveById(Class JavaDoc clazz, Serializable JavaDoc s) throws DAOException {
116         Session session = null;
117         try{
118             session = HibernateUtil.getSession();
119         }catch (ExceptionInInitializerError JavaDoc e){
120             throw new DAOException("Error in Hibernate Session instanciation", e);
121         }catch (Throwable JavaDoc e){
122             throw new DAOException("Error in Hibernate Session instanciation", e);
123         }
124         
125         Object JavaDoc 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 JavaDoc 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 JavaDoc<Object JavaDoc> retrieveAll(Class JavaDoc clazz) throws DAOException {
142         Session session = null;
143         try{
144             session = HibernateUtil.getSession();
145         }catch (ExceptionInInitializerError JavaDoc e){
146             throw new DAOException("Error in Hibernate Session instanciation", e);
147         }
148         catch (Exception JavaDoc e){
149             throw new DAOException("Error in Hibernate Session instanciation", e);
150         }
151         try {
152             List JavaDoc list = session.createQuery("from "+clazz.getName()).list();
153             List JavaDoc<Object JavaDoc> result = new ArrayList JavaDoc<Object JavaDoc>();
154             for (Iterator JavaDoc iter = list.iterator();iter.hasNext();){
155                 result.add((Object JavaDoc) iter.next());
156             }
157             return result;
158         }catch (Exception JavaDoc 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         //return session.createQuery("from :clazz").setString("clazz", clazz.getName()).list();
165
}
166
167 }
168
Popular Tags