1 /***
2 * @(#)AbstractHibernateDAO.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.dao;
26
27 import java.io.Serializable;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import net.sf.hibernate.HibernateException;
32 import net.sf.hibernate.LockMode;
33 import net.sf.hibernate.Session;
34 import net.sf.hibernate.type.Type;
35
36 import org.huihoo.jfox.soaf.container.ServiceFactory;
37 import org.huihoo.jfox.soaf.exception.DAOException;
38 import org.huihoo.jfox.soaf.services.persistence.HibernateService;
39
40 /***
41 * <p>
42 * Abstract Hibernate DAO Template.
43 * </p>
44 *
45 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
46 * @version $Revision: 1.1 $ $Date: 2005/05/22 06:49:10 $
47 * @version Revision: 1.0
48 */
49
50 public abstract class AbstractHibernateDAO extends AbstractDAO {
51
52 private HibernateService hibernateService;
53
54 /***
55 * Default Construector.
56 */
57 public AbstractHibernateDAO() {
58 hibernateService = (HibernateService) ServiceFactory.getInstance()
59 .getService(HibernateService.class);
60 }
61
62 /***
63 * Check if this instance is associated with this Session
64 *
65 * @param object an instance of a persistent class
66 * @return true if the given instance is associated with this Session
67 * @throws HibernateException
68 * @throws DAOException
69 */
70 public boolean contains(Object object) throws HibernateException,
71 DAOException {
72 boolean isContains = false;
73 Session session;
74 try {
75 session = hibernateService.openSession();
76 session.contains(object);
77 hibernateService.commit();
78 } catch (Exception e) {
79 hibernateService.rollback();
80 throw new DAOException(e);
81 } finally {
82 session = null;
83 hibernateService.closeSession();
84 }
85 return isContains;
86 }
87
88 /***
89 * Remove a persistent instance from the datastore.
90 *
91 * @param object
92 * @throws HibernateException
93 */
94 public void delete(Object object) throws HibernateException, DAOException {
95 Session session;
96 try {
97 session = hibernateService.openSession();
98 session.delete(object);
99 hibernateService.commit();
100 } catch (Exception e) {
101 hibernateService.rollback();
102 throw new DAOException(e);
103 } finally {
104 session = null;
105 hibernateService.closeSession();
106 }
107 }
108
109 /***
110 * Remove a persistent instance from the datastore with lock mode.
111 *
112 * @param object
113 * @param lockMode
114 * @throws HibernateException
115 */
116 public void delete(Object object, LockMode lockMode)
117 throws HibernateException, DAOException {
118 Session session;
119 try {
120 session = hibernateService.openSession();
121 session.lock(object, lockMode);
122 session.delete(object);
123 hibernateService.commit();
124 } catch (Exception e) {
125 hibernateService.rollback();
126 throw new HibernateException(e);
127 } finally {
128 session = null;
129 hibernateService.closeSession();
130 }
131 }
132
133 /***
134 * Delete all objects returned by the query. Return the number of objects
135 * deleted.
136 *
137 * @param query
138 * @return the number of instances deleted
139 * @throws HibernateException
140 * @throws DAOException
141 */
142 public int delete(String query) throws HibernateException, DAOException {
143 int instancesCount = 0;
144 Session session;
145 try {
146 session = hibernateService.openSession();
147 instancesCount = session.delete(query);
148 hibernateService.commit();
149 } catch (Exception e) {
150 hibernateService.rollback();
151 throw new DAOException(e);
152 } finally {
153 session = null;
154 hibernateService.closeSession();
155 }
156 return instancesCount;
157 }
158
159 /***
160 * Delete all objects returned by the query. Return the number of objects
161 * deleted.
162 *
163 * @param query
164 * @return the number of instances deleted
165 * @throws HibernateException
166 * @throws DAOException
167 */
168 public int delete(String query, Object value, Type type)
169 throws HibernateException, DAOException {
170 int instancesCount = 0;
171 Session session;
172 try {
173 session = hibernateService.openSession();
174 instancesCount = session.delete(query, value, type);
175 hibernateService.commit();
176 } catch (Exception e) {
177 hibernateService.rollback();
178 throw new DAOException(e);
179 } finally {
180 session = null;
181 hibernateService.closeSession();
182 }
183 return instancesCount;
184 }
185
186 /***
187 * Delete all objects returned by the query. Return the number of objects
188 * deleted.
189 *
190 * @param query
191 * @return the number of instances deleted
192 * @throws HibernateException
193 * @throws DAOException
194 */
195 public int delete(String query, Object[] values, Type[] types)
196 throws HibernateException, DAOException {
197 int instancesCount = 0;
198 Session session;
199 try {
200 session = hibernateService.openSession();
201 instancesCount = session.delete(query, values, types);
202 hibernateService.commit();
203 } catch (Exception e) {
204 hibernateService.rollback();
205 throw new DAOException(e);
206 } finally {
207 session = null;
208 hibernateService.closeSession();
209 }
210 return instancesCount;
211 }
212
213 /***
214 * Remove this instance from the session cache. Changes to the instance will
215 * not be synchronized with the database. This operation cascades to
216 * associated instances if the association is mapped with cascade="all" or
217 * cascade="all-delete-orphan".
218 *
219 * @param object object - a persistent instance
220 * @throws HibernateException
221 * @throws DAOException
222 */
223 public void evict(Object object) throws HibernateException, DAOException {
224 Session session;
225 try {
226 session = hibernateService.openSession();
227 session.evict(object);
228 hibernateService.commit();
229 } catch (Exception e) {
230 hibernateService.rollback();
231 throw new DAOException(e);
232 } finally {
233 session = null;
234 hibernateService.closeSession();
235 }
236 }
237
238 /***
239 * Execute a query.
240 *
241 * @param query a query expressed in Hibernate's query language
242 * @return a distinct list of instances (or arrays of instances)
243 * @throws HibernateException
244 * @throws DAOException
245 */
246 public List find(String query) throws HibernateException, DAOException {
247 List list;
248 Session session;
249 try {
250 session = hibernateService.openSession();
251 list = session.find(query);
252 hibernateService.commit();
253 } catch (Exception e) {
254 hibernateService.rollback();
255 throw new DAOException(e);
256 } finally {
257 session = null;
258 hibernateService.closeSession();
259 }
260 return list;
261 }
262
263 /***
264 * Execute a query with bind parameters. Bind a value to a "?" parameter in
265 * the query string.
266 *
267 * @param query
268 * @param value
269 * @param type
270 * @return a distinct list of instances (or arrays of instances)
271 * @throws HibernateException
272 * @throws DAOException
273 */
274 public List find(String query, Object value, Type type)
275 throws HibernateException, DAOException {
276 List list;
277 Session session;
278 try {
279 session = hibernateService.openSession();
280 list = session.find(query, value, type);
281 hibernateService.commit();
282 } catch (Exception e) {
283 hibernateService.rollback();
284 throw new DAOException(e);
285 } finally {
286 session = null;
287 hibernateService.closeSession();
288 }
289 return list;
290 }
291
292 /***
293 * Execute a query with bind parameters. Binding an array of values to "?"
294 * parameters in the query string.
295 *
296 * @param query
297 * @param values
298 * @param types
299 * @return a distinct list of instances
300 * @throws HibernateException
301 * @throws DAOException
302 */
303 public List find(String query, Object[] values, Type[] types)
304 throws HibernateException, DAOException {
305 List list;
306 Session session;
307 try {
308 session = hibernateService.openSession();
309 list = session.find(query, values, types);
310 hibernateService.commit();
311 } catch (Exception e) {
312 hibernateService.rollback();
313 throw new DAOException(e);
314 } finally {
315 session = null;
316 hibernateService.closeSession();
317 }
318 return list;
319 }
320
321 /***
322 * Return the persistent instance of the given entity class with the given
323 * identifier, or null if there is no such persistent instance.
324 *
325 * @param clazz
326 * @param id
327 * @return a persistent instance or null
328 * @throws HibernateException
329 * @throws DAOException
330 */
331 public Object get(Class clazz, Serializable id) throws HibernateException,
332 DAOException {
333 Object object;
334 Session session;
335 try {
336 session = hibernateService.openSession();
337 object = session.get(clazz, id);
338 hibernateService.commit();
339 } catch (Exception e) {
340 hibernateService.rollback();
341 throw new DAOException(e);
342 } finally {
343 session = null;
344 hibernateService.closeSession();
345 }
346 return object;
347 }
348
349 /***
350 * Return the persistent instance of the given entity class with the given
351 * identifier, or null if there is no such persistent instance. Obtain the
352 * specified lock mode if the instance exists.
353 *
354 * @param clazz
355 * @param id
356 * @param lockMode
357 * @return a persistent instance or null
358 * @throws HibernateException
359 * @throws DAOException
360 */
361 public Object get(Class clazz, Serializable id, LockMode lockMode)
362 throws HibernateException, DAOException {
363 Object object;
364 Session session;
365 try {
366 session = hibernateService.openSession();
367 object = session.get(clazz, id, lockMode);
368 hibernateService.commit();
369 } catch (Exception e) {
370 hibernateService.rollback();
371 throw new DAOException(e);
372 } finally {
373 session = null;
374 hibernateService.closeSession();
375 }
376 return object;
377 }
378
379 /***
380 * Execute a query and return the results in an iterator. If the query has
381 * multiple return values, values will be returned in an array of type
382 * Object[]. Entities returned as results are initialized on demand. The
383 * first SQL query returns identifiers only. So iterate() is usually a less
384 * efficient way to retrieve objects than find().
385 *
386 * @param query
387 * @return an iterator
388 * @throws HibernateException
389 * @throws DAOException
390 */
391 public Iterator iterate(String query) throws HibernateException,
392 DAOException {
393 Iterator iter;
394 Session session;
395 try {
396 session = hibernateService.openSession();
397 iter = session.iterate(query);
398 hibernateService.commit();
399 } catch (Exception e) {
400 hibernateService.rollback();
401 throw new DAOException(e);
402 } finally {
403 session = null;
404 hibernateService.closeSession();
405 }
406 return iter;
407 }
408
409 /***
410 * Execute a query and return the results in an iterator. Write the given
411 * value to "?" in the query string. If the query has multiple return
412 * values, values will be returned in an array of type Object[]. Entities
413 * returned as results are initialized on demand. The first SQL query
414 * returns identifiers only. So iterate() is usually a less efficient way to
415 * retrieve objects than find().
416 *
417 * @param query - the query string
418 * @param value - a value to be witten to a "?" placeholder in the query
419 * string
420 * @param type - the hibernate type of value
421 * @return an iterator
422 * @throws HibernateException
423 * @throws DAOException
424 */
425 public Iterator iterate(String query, Object value, Type type)
426 throws HibernateException, DAOException {
427 Iterator iter;
428 Session session;
429 try {
430 session = hibernateService.openSession();
431 iter = session.iterate(query, value, type);
432 hibernateService.commit();
433 } catch (Exception e) {
434 hibernateService.rollback();
435 throw new DAOException(e);
436 } finally {
437 session = null;
438 hibernateService.closeSession();
439 }
440 return iter;
441 }
442
443 /***
444 * Execute a query and return the results in an iterator. Write the given
445 * values to "?" in the query string. If the query has multiple return
446 * values, values will be returned in an array of type Object[]. Entities
447 * returned as results are initialized on demand. The first SQL query
448 * returns identifiers only. So iterate() is usually a less efficient way to
449 * retrieve objects than find().
450 *
451 * @param query
452 * @param values
453 * @param types
454 * @return
455 * @throws HibernateException
456 * @throws DAOException
457 */
458 public Iterator iterate(String query, Object[] values, Type[] types)
459 throws HibernateException, DAOException {
460 Iterator iter;
461 Session session;
462 try {
463 session = hibernateService.openSession();
464 iter = session.iterate(query, values, types);
465 hibernateService.commit();
466 } catch (Exception e) {
467 hibernateService.rollback();
468 throw new DAOException(e);
469 } finally {
470 session = null;
471 hibernateService.closeSession();
472 }
473 return iter;
474 }
475
476 /***
477 * Return the persistent instance of the given entity class with the given
478 * identifier, assuming that the instance exists. You should not use this
479 * method to determine if an instance exists (use find() instead). Use this
480 * only to retrieve an instance that you assume exists, where non-existence
481 * would be an actual error.
482 *
483 * @param clazz
484 * @param id
485 * @return the persistent instance or proxy
486 * @throws HibernateException
487 * @throws DAOException
488 */
489 public Object load(Class clazz, Serializable id) throws HibernateException,
490 DAOException {
491 Object object;
492 Session session;
493 try {
494 session = hibernateService.openSession();
495 object = session.load(clazz, id);
496 hibernateService.commit();
497 } catch (Exception e) {
498 hibernateService.rollback();
499 throw new DAOException(e);
500 } finally {
501 session = null;
502 hibernateService.closeSession();
503 }
504 return object;
505 }
506
507 /***
508 * Return the persistent instance of the given entity class with the given
509 * identifier, obtaining the specified lock mode, assuming the instance
510 * exists.
511 *
512 * @param clazz
513 * @param id
514 * @param lockMode
515 * @return the persistent instance or proxy
516 * @throws HibernateException
517 * @throws DAOException
518 */
519 public Object load(Class clazz, Serializable id, LockMode lockMode)
520 throws HibernateException, DAOException {
521 Object object;
522 Session session;
523 try {
524 session = hibernateService.openSession();
525 object = session.load(clazz, id, lockMode);
526 hibernateService.commit();
527 } catch (Exception e) {
528 hibernateService.rollback();
529 throw new DAOException(e);
530 } finally {
531 session = null;
532 hibernateService.closeSession();
533 }
534 return object;
535 }
536
537 /***
538 * Read the persistent state associated with the given identifier into the
539 * given transient instance.
540 *
541 * @param object
542 * @param id
543 * @throws HibernateException
544 * @throws DAOException
545 */
546 public void load(Object object, Serializable id) throws HibernateException,
547 DAOException {
548 Session session;
549 try {
550 session = hibernateService.openSession();
551 session.load(object, id);
552 hibernateService.commit();
553 } catch (Exception e) {
554 hibernateService.rollback();
555 throw new DAOException(e);
556 } finally {
557 session = null;
558 hibernateService.closeSession();
559 }
560 }
561
562 /***
563 * Obtain the specified lock level upon the given object.
564 *
565 * @param object - a persistent or transient instance
566 * @param lockMode - the lock level
567 * @throws HibernateException
568 * @throws DAOException
569 */
570 public void lock(Object object, LockMode lockMode)
571 throws HibernateException, DAOException {
572 Session session;
573 try {
574 session = hibernateService.openSession();
575 session.lock(object, lockMode);
576 hibernateService.commit();
577 } catch (Exception e) {
578 hibernateService.rollback();
579 throw new DAOException(e);
580 } finally {
581 session = null;
582 hibernateService.closeSession();
583 }
584 }
585
586 /***
587 * Update the persistent instance with the identifier of the given transient
588 * instance. If there is a persistent instance with the same identifier, an
589 * exception is thrown. If the given transient instance has a null
590 * identifier, an exception will be thrown.
591 *
592 * @param object - a transient instance containing updated state
593 * @throws HibernateException
594 * @throws DAOException
595 */
596 public void update(Object object) throws HibernateException, DAOException {
597 Session session;
598 try {
599 session = hibernateService.openSession();
600 session.update(object);
601 hibernateService.commit();
602 } catch (Exception e) {
603 hibernateService.rollback();
604 throw new DAOException(e);
605 } finally {
606 session = null;
607 hibernateService.closeSession();
608 }
609 }
610
611 /***
612 * Update the persistent state associated with the given identifier. An
613 * exception is thrown if there is a persistent instance with the same
614 * identifier in the current session.
615 *
616 * @param object - a transient instance containing updated state
617 * @param id - identifier of persistent instance
618 * @throws HibernateException
619 * @throws DAOException
620 */
621 public void update(Object object, Serializable id)
622 throws HibernateException, DAOException {
623 Session session;
624 try {
625 session = hibernateService.openSession();
626 session.update(object, id);
627 hibernateService.commit();
628 } catch (Exception e) {
629 hibernateService.rollback();
630 throw new DAOException(e);
631 } finally {
632 session = null;
633 hibernateService.closeSession();
634 }
635 }
636
637 /***
638 * Copy the state of the given object onto the persistent object with the
639 * same identifier. If there is no persistent instance currently associated
640 * with the session, it will be loaded. Return the persistent instance. If
641 * the given instance is unsaved or does not exist in the database, save it
642 * and return it as a newly persistent instance. Otherwise, the given
643 * instance does not become associated with the session.
644 *
645 * @param object
646 * @return an updated persistent instance
647 * @throws HibernateException
648 * @throws DAOException
649 */
650 public Object saveOrUpdateCopy(Object object) throws HibernateException,
651 DAOException {
652 Session session;
653 Object obj;
654 try {
655 session = hibernateService.openSession();
656 obj = session.saveOrUpdateCopy(object);
657 hibernateService.commit();
658 } catch (Exception e) {
659 hibernateService.rollback();
660 throw new DAOException(e);
661 } finally {
662 session = null;
663 hibernateService.closeSession();
664 }
665 return obj;
666 }
667
668 /***
669 * Copy the state of the given object onto the persistent object with the
670 * given identifier. If there is no persistent instance currently associated
671 * with the session, it will be loaded. Return the persistent instance. If
672 * there is no database row with the given identifier, save the given
673 * instance and return it as a newly persistent instance. Otherwise, the
674 * given instance does not become associated with the session.
675 *
676 * @param object - a persistent or transient instance with state to be
677 * copied
678 * @param id - the identifier of the instance to copy to
679 * @return an updated persistent instance
680 * @throws HibernateException
681 * @throws DAOException
682 */
683 public Object saveOrUpdateCopy(Object object, Serializable id)
684 throws HibernateException, DAOException {
685 Session session;
686 Object obj;
687 try {
688 session = hibernateService.openSession();
689 obj = session.saveOrUpdateCopy(object, id);
690 hibernateService.commit();
691 } catch (Exception e) {
692 hibernateService.rollback();
693 throw new DAOException(e);
694 } finally {
695 session = null;
696 hibernateService.closeSession();
697 }
698 return obj;
699 }
700
701 /***
702 * Re-read the state of the given instance from the underlying database. It
703 * is inadvisable to use this to implement long-running sessions that span
704 * many business tasks. This method is, however, useful in certain special
705 * circumstances.
706 *
707 * @param object a persistent or transient instance
708 * @throws HibernateException
709 * @throws DAOException
710 */
711 public void refresh(Object object) throws HibernateException, DAOException {
712 Session session;
713 try {
714 session = hibernateService.openSession();
715 session.refresh(object);
716 hibernateService.commit();
717 } catch (Exception e) {
718 hibernateService.rollback();
719 throw new DAOException(e);
720 } finally {
721 session = null;
722 hibernateService.closeSession();
723 }
724 }
725
726 /***
727 * Re-read the state of the given instance from the underlying database,
728 * with the given LockMode. It is inadvisable to use this to implement
729 * long-running sessions that span many business tasks. This method is,
730 * however, useful in certain special circumstances.
731 *
732 * @param object a persistent or transient instance
733 * @param lockMode the lock mode to use
734 * @throws HibernateException
735 * @throws DAOException
736 */
737 public void refresh(Object object, LockMode lockMode)
738 throws HibernateException, DAOException {
739 Session session;
740 try {
741 session = hibernateService.openSession();
742 session.refresh(object, lockMode);
743 hibernateService.commit();
744 } catch (Exception e) {
745 hibernateService.rollback();
746 throw new DAOException(e);
747 } finally {
748 session = null;
749 hibernateService.closeSession();
750 }
751 }
752
753 /***
754 * Persist the given transient instance, first assigning a generated
755 * identifier.
756 *
757 * @param object - - a transient instance of a persistent class
758 * @return the generated identifier
759 * @throws HibernateException
760 */
761 public Serializable save(Object object) throws HibernateException,
762 DAOException {
763 Serializable seriaObj;
764 Session session;
765 try {
766 session = hibernateService.openSession();
767 seriaObj = session.save(object);
768 hibernateService.commit();
769 } catch (Exception e) {
770 hibernateService.rollback();
771 throw new DAOException(e);
772 } finally {
773 session = null;
774 hibernateService.closeSession();
775 }
776 return seriaObj;
777 }
778
779 /***
780 * Persist the given transient instance, using the given identifier.
781 *
782 * @param object - a transient instance of a persistent class
783 * @param id - an unused valid identifier
784 * @throws HibernateException
785 * @throws DAOException
786 */
787 public void save(Object object, Serializable id) throws HibernateException,
788 DAOException {
789 Session session;
790 try {
791 session = hibernateService.openSession();
792 session.save(object, id);
793 hibernateService.commit();
794 } catch (Exception e) {
795 hibernateService.rollback();
796 throw new DAOException(e);
797 } finally {
798 System.out.println("save close session");
799 session = null;
800 hibernateService.closeSession();
801 }
802 }
803
804 /***
805 * Either save() or update() the given instance, depending upon the value of
806 * its identifier property. By default the instance is always saved. This
807 * behaviour may be adjusted by specifying an unsaved-value attribute of the
808 * identifier property mapping.
809 *
810 * @param object - a transient instance containing new or updated state
811 * @throws HibernateException
812 * @throws DAOException
813 */
814 public void saveOrUpdate(Object object) throws HibernateException,
815 DAOException {
816 Session session;
817 try {
818 session = hibernateService.openSession();
819 session.saveOrUpdate(object);
820 hibernateService.commit();
821 } catch (Exception e) {
822 hibernateService.rollback();
823 throw new DAOException(e);
824 } finally {
825 session = null;
826 hibernateService.closeSession();
827 }
828 }
829
830 }