SSH问题及解决之三 SpringSide3缺陷

http://code.google.com/p/springside/issues/detail?id=28
[quote]ss3中的PropertyFilter 好像没提供实现where 属性名1 like '%值1%' or 属性名1
like '%值2%' or 属性名1 like '%值3%' ...这种情况的
请问遇到这种情况该怎么实现呢,如果自己写就要丢掉page封装了[/quote]

我的项目也遇到这个问题, 而SpringSide确实没有提供好的解决方案,原因是PropertyFilter中的propertyValue, matchType没有使用数据类型进行声明定义, 而propertyNames是数组, 这就导至了, '或'(_OR_)操作时值和匹配类型必须一致.
我的做法是:
		// 1. '不等于' '或' '非空'的逻辑
Disjunction disjunction = Restrictions.disjunction();
// 不等于
Criterion neCriterion = buildPropertyFilterCriterion("workCenterId",
workCenterId, java.lang.Integer.class,
PropertyFilter.MatchType.NE);
// 或为空
Criterion nullCriterion = buildPropertyFilterCriterion("workCenterId",
null, java.lang.Integer.class, PropertyFilter.MatchType.NULL);
disjunction.add(neCriterion);
disjunction.add(nullCriterion);
finalList.add(disjunction);
// 2. 追加获得到的查询条件
List criterionList = this.buildCriterions(filters);
if (criterionList!=null && !criterionList.isEmpty()) {
finalList.addAll(criterionList);
}
Criterion[] criterions = finalList
.toArray(new Criterion[finalList.size()]);
return findPage(page, criterions);


如上, 由于项目进度关系没有去扩展ss, 对于这种或的关系自己做了较硬性的编码。

	/**
* 获得查询条件
*
* @param filters
* @return
*/
protected List buildCriterions(final List filters) {
List criterionList = new ArrayList();
for (PropertyFilter filter : filters) {
if (!filter.isMultiProperty()) { // There is only one property
// need to compare the
// situation.
Criterion criterion = buildPropertyFilterCriterion(filter
.getPropertyName(), filter.getPropertyValue(), filter
.getPropertyType(), filter.getMatchType());
criterionList.add(criterion);
} else {// Contain multiple attributes need to compare the situation
// to carry out or deal with.
Disjunction disjunction = Restrictions.disjunction();
for (String param : filter.getPropertyNames()) {
Criterion criterion = buildPropertyFilterCriterion(param,
filter.getPropertyValue(),
filter.getPropertyType(), filter.getMatchType());
disjunction.add(criterion);
}
criterionList.add(disjunction);
}
}
return criterionList;
}

你可能感兴趣的:(J2EE开发)