1. 程式人生 > >Hibernate級聯刪除

Hibernate級聯刪除

  在使用Hibernate框架的時候,有時候會用到級聯刪除:在刪除主表記錄前,將記錄所關聯的表的資訊一併刪除,這裡沒有用到配置,而是邏輯來實現 。

/* 刪除
	 */
	@Transactional
	public boolean delOper(Long operId){
		boolean status = false;
		try{
			//刪t_operator表記錄之前先要刪除t_sys_log表所關聯的記錄
			List<Object> params = new ArrayList<Object>(); 
			StringBuffer hql = new StringBuffer();
			hql.append(" delete from t_sys_log l where l.created_by=?");
			params.add(operId);			
			hibernateDao.updateBySql(hql.toString(), params.toArray());
			hibernateDao.delete(TOperator.class, operId);
			status = true;
		}catch(Exception ex){
			logger.error(ErrorCodeConstants.ERROR_1_DB, ErrorCodeConstants.ERROR_DB_2);
			ex.printStackTrace();
		}	
		return status;
	}