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