private Entity<? extends Serializable> onBeforeSave(
			Entity<? extends Serializable> entity) {
		Class<?> cls = entity.getClass();
		String indexName = DaoHelper.getExtendFieldName(cls, ExtendField.Index);
		String pathName = DaoHelper.getExtendFieldName(cls, ExtendField.Path);
		String parentName = DaoHelper.getExtendFieldName(cls,
				ExtendField.Parent);

		// 可记录时间的
		if (Dateable.class.isAssignableFrom(cls)) {
			Date now = Calendar.getInstance().getTime();
			((Dateable) entity).setCreateDate(now);
			((Dateable) entity).setLastModifiedDate(now);
		}

		// 可树形结构化的
		if (Treeable.class.isAssignableFrom(cls)) {
			Treeable self = ((Treeable) entity);
			// 获取上级节点
			Serializable parentId = self.getParent();

			// 设置节点自身的路径
			if (isNull(parentId)) {
				self.setPath("/"); // 没有上级节点的节点
				// 设置节点自身的排位
				Object maxIndex = createCriteria(cls)
						.add(Restrictions.eq(pathName, "/"))
						.setProjection(Projections.max(indexName))
						.uniqueResult();
				self.setIndex(isNull(maxIndex) ? 1 : Integer.valueOf(maxIndex
						.toString()) + 1);
			} else {
				// 设置节点自身的排位
				Object maxIndex = createCriteria(cls)
						.add(Restrictions.eq(parentName, parentId))
						.setProjection(Projections.max(indexName))
						.uniqueResult();
				self.setIndex(isNull(maxIndex) ? 1 : Integer.valueOf(maxIndex
						.toString()) + 1);
			}
		}
		// 可移动的
		else if (Movable.class.isAssignableFrom(cls)) {
			// 设置自身的排位
			Integer maxIndex = 1;
			Object maxVal = createCriteria(cls).setProjection(
					Projections.max(indexName)).uniqueResult();
			if (notNull(maxVal)) {
				maxIndex = Integer.valueOf(maxVal.toString()) + 1;
			}
			((Movable) entity).setIndex(maxIndex);
		}

		return entity;
	}
private Entity < ? extends Serializable > onBeforeSave ( Entity < ? extends Serializable > entity ) { Class < ? > cls = entity . getClass ( ) ; String indexName = DaoHelper . getExtendFieldName ( cls , ExtendField . Index ) ; String pathName = DaoHelper . getExtendFieldName ( cls , ExtendField . Path ) ; String parentName = DaoHelper . getExtendFieldName ( cls , ExtendField . Parent ) ; if ( Dateable . class . isAssignableFrom ( cls ) ) { Date now = Calendar . getInstance ( ) . getTime ( ) ; ( ( Dateable ) entity ) . setCreateDate ( now ) ; ( ( Dateable ) entity ) . setLastModifiedDate ( now ) ; } if ( Treeable . class . isAssignableFrom ( cls ) ) { Treeable self = ( ( Treeable ) entity ) ; Serializable parentId = self . getParent ( ) ; if ( isNull ( parentId ) ) { self . setPath ( "/" ) ; Object maxIndex = createCriteria ( cls ) . add ( Restrictions . eq ( pathName , "/" ) ) . setProjection ( Projections . max ( indexName ) ) . uniqueResult ( ) ; self . setIndex ( isNull ( maxIndex ) ? 1 : Integer . valueOf ( maxIndex . toString ( ) ) + 1 ) ; } else { Object maxIndex = createCriteria ( cls ) . add ( Restrictions . eq ( parentName , parentId ) ) . setProjection ( Projections . max ( indexName ) ) . uniqueResult ( ) ; self . setIndex ( isNull ( maxIndex ) ? 1 : Integer . valueOf ( maxIndex . toString ( ) ) + 1 ) ; } } else if ( Movable . class . isAssignableFrom ( cls ) ) { Integer maxIndex = 1 ; Object maxVal = createCriteria ( cls ) . setProjection ( Projections . max ( indexName ) ) . uniqueResult ( ) ; if ( notNull ( maxVal ) ) { maxIndex = Integer . valueOf ( maxVal . toString ( ) ) + 1 ; } ( ( Movable ) entity ) . setIndex ( maxIndex ) ; } return entity ; }
新增实体之前的处理

@param entity
@return