[英]Hibernate criteria accepting %% value
I am using the below Hibernate code to filter workFlowName
. 我使用下面的Hibernate代码来过滤
workFlowName
。
crt.add(Restrictions.like("workFlowName", workFlow, MatchMode.ANYWHERE));
// crt is the criteria
The problem is when I pass the value to workFlow from web (TextBox).it fetching the value correctly (I am passing sym
in text box if fetch 10 records.if i pass the same value like %sym%
again it is fetching same record). 问题是当我从web(TextBox)传递值到workFlow时 。正确获取值(如果获取10条记录,我在文本框中传递
sym
。如果我再次传递相同的值,如%sym%
,它将获取相同的记录)。
Whether the criteria will omit when it see %%
as argument? 当
%%
作为参数时,标准是否会省略?
Hibernate does not escape special chars in like (eg the percentage %
sign). Hibernate不逃避等 (例如百分比特殊字符
%
标志)。 But there are descriptions how to solve it (escape it) : 但是有描述如何解决它(逃避它) :
In general we could implement our own EscapedILikeExpression
(taken from the first link above) 一般来说,我们可以实现自己的
EscapedILikeExpression
(取自上面的第一个链接)
class EscapedILikeExpression extends IlikeExpression {
private static final String HIBERNATE_ESCAPE_CHAR = "\\";
public EscapedILikeExpression(String propertyName, Object value) {
super(propertyName, value);
}
public EscapedILikeExpression(String propertyName, String value, MatchMode matchMode) {
super(propertyName, replaceAll(value), matchMode);
}
private static String replaceAll(String value) {
return value
.replace("\\", HIBERNATE_ESCAPE_CHAR + "\\")
.replace("_", HIBERNATE_ESCAPE_CHAR + "_")
.replace("%", HIBERNATE_ESCAPE_CHAR + "%");
}
}
and 和
public class EscapedRestrictions {
public static Criterion ilike(String propertyName, String value) {
return new EscapedILikeExpression(propertyName, value);
}
public static Criterion ilike(String propertyName, String value, MatchMode matchMode) {
return new EscapedILikeExpression(propertyName, value, matchMode);
}
}
And now we should be able to call 而现在我们应该可以打电话了
crt.add(EscapedRestrictions.ilike("workFlowName", workFlow, MatchMode.ANYWHERE));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.