山寨级联删除

//删除国家的时候同时删除该国的省和城市
	public int deleteAllByLevelThree(String[] Ids){

		String tempMainIds = Arrays.toString(Ids).replaceAll("\\u005B", "").replaceAll("\\u005D", "");
		String subIds = ""; //provinces
		String subsubIds = ""; //cities
		
		String sqlstr = " from BaseTypeEntity where parentId in(" + tempMainIds +")";
		List<BaseTypeEntity> btypes = this.find(sqlstr);

		for(BaseTypeEntity btype : btypes){
			subIds += btype.getId().toString() + ",";
		}
		
		sqlstr = " from BaseTypeEntity where parentId in(" + subIds.substring(0, subIds.length()-1) +")";
		btypes = this.find(sqlstr);
		for(BaseTypeEntity btype : btypes){
			subsubIds += btype.getId().toString() + ",";
		}
		
		String deleteIds = "";
		if(!"".equals(tempMainIds)){
			deleteIds += tempMainIds + ",";
		}
		
		if(!"".equals(subIds)){
			deleteIds += subIds;
		}
		
		if(!"".equals(subsubIds)){
			deleteIds += subsubIds;
		}
		
		if(!"".equals(deleteIds)){
			deleteIds = deleteIds.substring(0, deleteIds.length()-1);
			sqlstr = "delete from BaseTypeEntity where id in ("+deleteIds+")";
		}
		
		Query query=this.getSession().createQuery(sqlstr);
		
		return query.executeUpdate();
	}

 

你可能感兴趣的:(删除)