View Javadoc

    1   /***
    2    * @(#)HibernateServiceImpl.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.persistence;
    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.SessionFactory;
    35  import net.sf.hibernate.Transaction;
    36  import net.sf.hibernate.type.Type;
    37
    38  import org.apache.commons.logging.Log;
    39  import org.apache.commons.logging.LogFactory;
    40  import org.huihoo.jfox.soaf.exception.DAOException;
    41
    42  /***
    43   * <p>
    44   * Hibernate persistence service implementation.
    45   * </p>
    46   *
    47   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    48   * @version $Revision: 1.11 $ $Date: 2006/02/15 08:45:45 $
    49   * @version Revision: 1.0
    50   */
    51
    52  public class HibernateServiceImpl implements HibernateService {
    53
    54  	private final Log logger = LogFactory.getLog(getClass());
    55
    56  	private Session session = null;
    57
    58      private Transaction transaction = null;
    59
    60  	private SessionFactory sessionFactory = null;
    61
    62  	/***
    63  	 * Default constructor.
    64  	 */
    65  	public HibernateServiceImpl() {
    66  	}
    67
    68  	/***
    69  	 * Set SessionFactory
    70  	 * @param sessionFactory
    71  	 */
    72  	public void setSessionFactory(SessionFactory sessionFactory) {
    73  		this.sessionFactory = sessionFactory;
    74  	}
    75
    76  	/***
    77  	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#openSession()
    78  	 */
    79  	public Session openSession() throws HibernateException {
    80  		if (session == null) {
    81  			session = sessionFactory.openSession();
    82  			transaction = session.beginTransaction();
    83  		}
    84  		return session;
    85  	}
    86
    87  	/***
    88  	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#closeSession()
    89  	 */
    90  	public void closeSession() throws HibernateException {
    91  		if (session != null) {
    92  			if (transaction != null && !transaction.wasCommitted()
    93  					&& !transaction.wasRolledBack()) {
    94  				transaction.commit();
    95  				transaction = null;
    96  			}
    97  			session.close();
    98  			session = null;
    99  		}
    100 	}
    101
    102 	/***
    103 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#commit()
    104 	 */
    105 	public void commit() throws HibernateException {
    106 		if (session != null && transaction != null
    107 				&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    108 			transaction.commit();
    109 			transaction = session.beginTransaction();
    110 		}
    111 	}
    112
    113 	/***
    114 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#reset()
    115 	 */
    116 	public void reset() {
    117 		try {
    118 			rollback();
    119 			if (session != null) {
    120 				session.clear();
    121 				session.close();
    122 			}
    123 		} catch (HibernateException ex) {
    124 			// we do nothing here, because this shall be called if something
    125 			// went wrong.
    126 		} finally {
    127 			session = null;
    128 			transaction = null;
    129 		}
    130
    131 	}
    132
    133 	/***
    134 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#rollback()
    135 	 */
    136 	public void rollback() throws HibernateException {
    137 		if (session != null && transaction != null
    138 				&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    139 			transaction.rollback();
    140 			transaction = session.beginTransaction();
    141 		}
    142 	}
    143
    144 	/***
    145 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#contains(Object)
    146 	 */
    147 	public boolean contains(Object object) throws HibernateException,
    148 			DAOException {
    149 		boolean isContains = false;
    150 		Session session = null;
    151 		try {
    152 			session = openSession();
    153 			session.contains(object);
    154 			commit();
    155 		} catch (Exception e) {
    156 			rollback();
    157 			throw new DAOException(e);
    158 		} finally {
    159 			session = null;
    160 			closeSession();
    161 		}
    162 		return isContains;
    163 	}
    164
    165 	/***
    166 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(Object)
    167 	 */
    168 	public void delete(Object object) throws HibernateException, DAOException {
    169 		Session session = null;
    170 		try {
    171 			session = openSession();
    172 			session.delete(object);
    173 			commit();
    174 		} catch (Exception e) {
    175 			rollback();
    176 			throw new DAOException(e);
    177 		} finally {
    178 			session = null;
    179 			closeSession();
    180 		}
    181 	}
    182
    183 	/***
    184 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(Object,
    185 	 *      LockMode)
    186 	 */
    187 	public void delete(Object object, LockMode lockMode)
    188 			throws HibernateException, DAOException {
    189 		Session session = null;
    190 		try {
    191 			session = openSession();
    192 			session.lock(object, lockMode);
    193 			session.delete(object);
    194 			commit();
    195 		} catch (Exception e) {
    196 			rollback();
    197 			throw new HibernateException(e);
    198 		} finally {
    199 			session = null;
    200 			closeSession();
    201 		}
    202 	}
    203
    204 	/***
    205 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String)
    206 	 */
    207 	public int delete(String query) throws HibernateException, DAOException {
    208 		int instancesCount = 0;
    209 		Session session = null;
    210 		try {
    211 			session = openSession();
    212 			instancesCount = session.delete(query);
    213 			commit();
    214 		} catch (Exception e) {
    215 			rollback();
    216 			throw new DAOException(e);
    217 		} finally {
    218 			session = null;
    219 			closeSession();
    220 		}
    221 		return instancesCount;
    222 	}
    223
    224 	/***
    225 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String,
    226 	 *      Object, Type)
    227 	 */
    228 	public int delete(String query, Object value, Type type)
    229 			throws HibernateException, DAOException {
    230 		int instancesCount = 0;
    231 		Session session = null;
    232 		try {
    233 			session = openSession();
    234 			instancesCount = session.delete(query, value, type);
    235 			commit();
    236 		} catch (Exception e) {
    237 			rollback();
    238 			throw new DAOException(e);
    239 		} finally {
    240 			session = null;
    241 			closeSession();
    242 		}
    243 		return instancesCount;
    244 	}
    245
    246 	/***
    247 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String,
    248 	 *      Object[], Type[])
    249 	 */
    250 	public int delete(String query, Object[] values, Type[] types)
    251 			throws HibernateException, DAOException {
    252 		int instancesCount = 0;
    253 		Session session = null;
    254 		try {
    255 			session = openSession();
    256 			instancesCount = session.delete(query, values, types);
    257 			commit();
    258 		} catch (Exception e) {
    259 			rollback();
    260 			throw new DAOException(e);
    261 		} finally {
    262 			session = null;
    263 			closeSession();
    264 		}
    265 		return instancesCount;
    266 	}
    267
    268 	/***
    269 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#evict(Object)
    270 	 */
    271 	public void evict(Object object) throws HibernateException, DAOException {
    272 		Session session;
    273 		try {
    274 			session = openSession();
    275 			session.evict(object);
    276 			commit();
    277 		} catch (Exception e) {
    278 			rollback();
    279 			throw new DAOException(e);
    280 		} finally {
    281 			session = null;
    282 			closeSession();
    283 		}
    284 	}
    285
    286 	/***
    287 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String)
    288 	 */
    289 	public List find(String query) throws HibernateException, DAOException {
    290 		List list = null;
    291 		Session session = null;
    292 		try {
    293 			session = openSession();
    294 			list = session.find(query);
    295 			commit();
    296 		} catch (Exception e) {
    297 			rollback();
    298 			throw new DAOException(e);
    299 		} finally {
    300 			session = null;
    301 			closeSession();
    302 		}
    303 		return list;
    304 	}
    305
    306 	/***
    307 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String,
    308 	 *      Object, Type)
    309 	 */
    310 	public List find(String query, Object value, Type type)
    311 			throws HibernateException, DAOException {
    312 		List list = null;
    313 		Session session;
    314 		try {
    315 			session = openSession();
    316 			list = session.find(query, value, type);
    317 			commit();
    318 		} catch (Exception e) {
    319 			rollback();
    320 			throw new DAOException(e);
    321 		} finally {
    322 			session = null;
    323 			closeSession();
    324 		}
    325 		return list;
    326 	}
    327
    328 	/***
    329 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String,
    330 	 *      Object[], Type[])
    331 	 */
    332 	public List find(String query, Object[] values, Type[] types)
    333 			throws HibernateException, DAOException {
    334 		List list = null;
    335 		Session session = null;
    336 		try {
    337 			session = openSession();
    338 			list = session.find(query, values, types);
    339 			commit();
    340 		} catch (Exception e) {
    341 			rollback();
    342 			throw new DAOException(e);
    343 		} finally {
    344 			session = null;
    345 			closeSession();
    346 		}
    347 		return list;
    348 	}
    349
    350 	/***
    351 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#get(Class,
    352 	 *      Serializable)
    353 	 */
    354 	public Object get(Class clazz, final Serializable id)
    355 			throws HibernateException, DAOException {
    356 		Object object;
    357 		Session session = null;
    358 		try {
    359 			session = openSession();
    360 			object = session.get(clazz, id);
    361 			commit();
    362 		} catch (Exception e) {
    363 			rollback();
    364 			throw new DAOException(e);
    365 		} finally {
    366 			session = null;
    367 			closeSession();
    368 		}
    369 		return object;
    370 	}
    371
    372 	/***
    373 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#get(Class,
    374 	 *      Serializable, LockMode)
    375 	 */
    376 	public Object get(Class clazz, final Serializable id, LockMode lockMode)
    377 			throws HibernateException, DAOException {
    378 		Object object;
    379 		Session session;
    380 		try {
    381 			session = openSession();
    382 			object = session.get(clazz, id, lockMode);
    383 			commit();
    384 		} catch (Exception e) {
    385 			rollback();
    386 			throw new DAOException(e);
    387 		} finally {
    388 			session = null;
    389 			closeSession();
    390 		}
    391 		return object;
    392 	}
    393
    394 	/***
    395 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String)
    396 	 */
    397 	public Iterator iterate(String query) throws HibernateException,
    398 			DAOException {
    399 		Iterator iter;
    400 		Session session = null;
    401 		try {
    402 			session = openSession();
    403 			iter = session.iterate(query);
    404 			commit();
    405 		} catch (Exception e) {
    406 			rollback();
    407 			throw new DAOException(e);
    408 		} finally {
    409 			session = null;
    410 			closeSession();
    411 		}
    412 		return iter;
    413 	}
    414
    415 	/***
    416 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String,
    417 	 *      Object, Type)
    418 	 */
    419 	public Iterator iterate(String query, Object value, Type type)
    420 			throws HibernateException, DAOException {
    421 		Iterator iter;
    422 		Session session = null;
    423 		try {
    424 			session = openSession();
    425 			iter = session.iterate(query, value, type);
    426 			commit();
    427 		} catch (Exception e) {
    428 			rollback();
    429 			throw new DAOException(e);
    430 		} finally {
    431 			session = null;
    432 			closeSession();
    433 		}
    434 		return iter;
    435 	}
    436
    437 	/***
    438 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String,
    439 	 *      Object[], Type[])
    440 	 */
    441 	public Iterator iterate(String query, Object[] values, Type[] types)
    442 			throws HibernateException, DAOException {
    443 		Iterator iter;
    444 		Session session;
    445 		try {
    446 			session = openSession();
    447 			iter = session.iterate(query, values, types);
    448 			commit();
    449 		} catch (Exception e) {
    450 			rollback();
    451 			throw new DAOException(e);
    452 		} finally {
    453 			session = null;
    454 			closeSession();
    455 		}
    456 		return iter;
    457 	}
    458
    459 	/***
    460 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class,
    461 	 *      Serializable)
    462 	 */
    463 	public Object load(Class clazz, final Serializable id)
    464 			throws HibernateException, DAOException {
    465 		Object object;
    466 		Session session = null;
    467 		try {
    468 			session = openSession();
    469 			object = session.load(clazz, id);
    470 			commit();
    471 		} catch (Exception e) {
    472 			rollback();
    473 			throw new DAOException(e);
    474 		} finally {
    475 			session = null;
    476 			closeSession();
    477 		}
    478 		return object;
    479 	}
    480
    481 	/***
    482 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class,
    483 	 *      Serializable, LockMode)
    484 	 */
    485 	public Object load(Class clazz, final Serializable id, LockMode lockMode)
    486 			throws HibernateException, DAOException {
    487 		Object object;
    488 		Session session;
    489 		try {
    490 			session = openSession();
    491 			object = session.load(clazz, id, lockMode);
    492 			commit();
    493 		} catch (Exception e) {
    494 			rollback();
    495 			throw new DAOException(e);
    496 		} finally {
    497 			session = null;
    498 			closeSession();
    499 		}
    500 		return object;
    501 	}
    502
    503 	/***
    504 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Object,
    505 	 *      Serializable)
    506 	 */
    507 	public void load(Object object, final Serializable id)
    508 			throws HibernateException, DAOException {
    509 		Session session = null;
    510 		try {
    511 			session = openSession();
    512 			session.load(object, id);
    513 			commit();
    514 		} catch (Exception e) {
    515 			rollback();
    516 			throw new DAOException(e);
    517 		} finally {
    518 			session = null;
    519 			closeSession();
    520 		}
    521 	}
    522
    523 	/***
    524 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class,
    525 	 *      Serializable, LockMode)
    526 	 */
    527 	public void lock(Object object, LockMode lockMode)
    528 			throws HibernateException, DAOException {
    529 		Session session;
    530 		try {
    531 			session = openSession();
    532 			session.lock(object, lockMode);
    533 			commit();
    534 		} catch (Exception e) {
    535 			rollback();
    536 			throw new DAOException(e);
    537 		} finally {
    538 			session = null;
    539 			closeSession();
    540 		}
    541 	}
    542
    543 	/***
    544 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#update(Object)
    545 	 */
    546 	public void update(Object object) throws HibernateException, DAOException {
    547 		Session session = null;
    548 		try {
    549 			session = openSession();
    550 			session.update(object);
    551 			commit();
    552 		} catch (Exception e) {
    553 			rollback();
    554 			throw new DAOException(e);
    555 		} finally {
    556 			session = null;
    557 			closeSession();
    558 		}
    559 	}
    560
    561 	/***
    562 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#update(Object,
    563 	 *      Serializable)
    564 	 */
    565 	public void update(Object object, final Serializable id)
    566 			throws HibernateException, DAOException {
    567 		Session session = null;
    568 		try {
    569 			session = openSession();
    570 			session.update(object, id);
    571 			commit();
    572 		} catch (Exception e) {
    573 			rollback();
    574 			throw new DAOException(e);
    575 		} finally {
    576 			session = null;
    577 			closeSession();
    578 		}
    579 	}
    580
    581 	/***
    582 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdate(Object)
    583 	 */
    584 	public Object saveOrUpdateCopy(Object object) throws HibernateException,
    585 			DAOException {
    586 		Session session;
    587 		Object obj;
    588 		try {
    589 			session = openSession();
    590 			obj = session.saveOrUpdateCopy(object);
    591 			commit();
    592 		} catch (Exception e) {
    593 			rollback();
    594 			throw new DAOException(e);
    595 		} finally {
    596 			session = null;
    597 			closeSession();
    598 		}
    599 		return obj;
    600 	}
    601
    602 	/***
    603 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdateCopy(Object,
    604 	 *      Serializable)
    605 	 */
    606 	public Object saveOrUpdateCopy(Object object, final Serializable id)
    607 			throws HibernateException, DAOException {
    608 		Session session = null;
    609 		Object obj;
    610 		try {
    611 			session = openSession();
    612 			obj = session.saveOrUpdateCopy(object, id);
    613 			commit();
    614 		} catch (Exception e) {
    615 			rollback();
    616 			throw new DAOException(e);
    617 		} finally {
    618 			session = null;
    619 			closeSession();
    620 		}
    621 		return obj;
    622 	}
    623
    624 	/***
    625 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#refresh(Object)
    626 	 */
    627 	public void refresh(Object object) throws HibernateException, DAOException {
    628 		Session session = null;
    629 		try {
    630 			session = openSession();
    631 			session.refresh(object);
    632 			commit();
    633 		} catch (Exception e) {
    634 			rollback();
    635 			throw new DAOException(e);
    636 		} finally {
    637 			session = null;
    638 			closeSession();
    639 		}
    640 	}
    641
    642 	/***
    643 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#refresh(Object,
    644 	 *      LockMode)
    645 	 */
    646 	public void refresh(Object object, LockMode lockMode)
    647 			throws HibernateException, DAOException {
    648 		Session session = null;
    649 		try {
    650 			session = openSession();
    651 			session.refresh(object, lockMode);
    652 			commit();
    653 		} catch (Exception e) {
    654 			rollback();
    655 			throw new DAOException(e);
    656 		} finally {
    657 			session = null;
    658 			closeSession();
    659 		}
    660 	}
    661
    662 	/***
    663 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#save(Object)
    664 	 */
    665 	public Serializable save(Object object) throws HibernateException,
    666 			DAOException {
    667 		Serializable seriaObj;
    668 		Session session = null;
    669 		try {
    670 			session = openSession();
    671 			seriaObj = session.save(object);
    672 			commit();
    673 		} catch (Exception e) {
    674 			rollback();
    675 			throw new DAOException(e);
    676 		} finally {
    677 			session = null;
    678 			closeSession();
    679 		}
    680 		return seriaObj;
    681 	}
    682
    683 	/***
    684 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#save(Object,
    685 	 *      Serializable)
    686 	 */
    687 	public void save(Object object, final Serializable id)
    688 			throws HibernateException, DAOException {
    689 		Session session;
    690 		try {
    691 			session = openSession();
    692 			session.save(object, id);
    693 			commit();
    694 		} catch (Exception e) {
    695 			rollback();
    696 			throw new DAOException(e);
    697 		} finally {
    698 			session = null;
    699 			closeSession();
    700 		}
    701 	}
    702
    703 	/***
    704 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdate(Object)
    705 	 */
    706 	public void saveOrUpdate(Object object) throws HibernateException,
    707 			DAOException {
    708 		Session session = null;
    709 		try {
    710 			session = openSession();
    711 			session.saveOrUpdate(object);
    712 			commit();
    713 		} catch (Exception e) {
    714 			rollback();
    715 			throw new DAOException(e);
    716 		} finally {
    717 			session = null;
    718 			closeSession();
    719 		}
    720 	}
    721
    722 }