hibernate 在使用休眠条件时,有没有一种方法可以转义特殊字符?

ao218c7q  于 12个月前  发布在  其他
关注(0)|答案(5)|浏览(143)

对于这个问题,我们希望避免编写一个特殊的查询,因为查询在多个数据库中必须是不同的。只使用hibernate条件,我们希望能够转义特殊字符。
这种情况就是需要转义特殊字符的原因:
假设我们在数据库中有表'foo'。表'foo'只包含一个字段,称为'name'。'name'字段可以包含在数据库中可能被认为是特殊的字符。这样的名称的两个例子是'name_1'和'name%1'。'_'和'%'都是特殊字符,至少在Oracle中是这样的。如果用户希望在这些示例输入数据库后搜索其中一个示例,则可能会出现问题。

criterion = Restrictions.ilike("name", searchValue, MatchMode.ANYWHERE);
return findByCriteria(null, criterion);

字符串
在这段代码中,'searchValue'是用户为应用程序提供的用于搜索的值。如果用户想要搜索' %',那么数据库中的每个'foo'条目都会返回给用户。这是因为'%'字符表示用于字符串匹配的“任意数量的字符”,Hibernate生成的SQL代码如下所示:

select * from foo where name like '%'


有没有一种方法可以告诉hibernate转义某些字符,或者创建一个不特定于数据库类型的解决方法?

ppcbkaq5

ppcbkaq51#

LikeExpression的构造函数都是受保护的,所以它不是一个可行的选择。而且,它有problems of its own
我和一个同事创建了一个补丁,它工作得很好。补丁的要点是,对于使用MatchMode的LikeExpression构造函数,我们转义了特殊字符。对于使用Character(转义字符)的构造函数,我们假设用户自己转义了特殊字符。
我们还参数化了转义字符,以确保它不会破坏SQL查询,如果它们使用类似\或引号的字符。

package org.hibernate.criterion;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.TypedValue;

public class LikeExpression implements Criterion {
    private final String propertyName;
    private final String value;
    private final Character escapeChar;

    protected LikeExpression(
            String propertyName,
            Object value) {
        this(propertyName, value.toString(), (Character) null);
    }

    protected LikeExpression(
            String propertyName,
            String value,
            MatchMode matchMode) {
        this( propertyName, matchMode.toMatchString( value
                .toString()
                .replaceAll("!", "!!")
                .replaceAll("%", "!%")
                .replaceAll("_", "!_")), '!' );
    }

    protected LikeExpression(
            String propertyName,
            String value,
            Character escapeChar) {
        this.propertyName = propertyName;
        this.value = value;
        this.escapeChar = escapeChar;
    }

    public String toSqlString(
            Criteria criteria,
            CriteriaQuery criteriaQuery) throws HibernateException {
        Dialect dialect = criteriaQuery.getFactory().getDialect();
        String[] columns = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
        if ( columns.length != 1 ) {
            throw new HibernateException( "Like may only be used with single-column properties" );
        }
        String lhs = lhs(dialect, columns[0]);
        return lhs + " like ?" + ( escapeChar == null ? "" : " escape ?" );

    }

    public TypedValue[] getTypedValues(
            Criteria criteria,
            CriteriaQuery criteriaQuery) throws HibernateException {
        return new TypedValue[] {
                criteriaQuery.getTypedValue( criteria, propertyName, typedValue(value) ),
                criteriaQuery.getTypedValue( criteria, propertyName, escapeChar.toString() )
        };
    }

    protected String lhs(Dialect dialect, String column) {
        return column;
    }

    protected String typedValue(String value) {
        return value;
    }

}

字符串
如果您想知道lhs和typedValue方法的用途,那么新的IlikeExpression应该可以回答这些问题。

package org.hibernate.criterion;

import org.hibernate.dialect.Dialect;

public class IlikeExpression extends LikeExpression {

    protected IlikeExpression(
            String propertyName,
            Object value) {
        super(propertyName, value);
    }

    protected IlikeExpression(
            String propertyName,
            String value,
            MatchMode matchMode) {
        super(propertyName, value, matchMode);

    }

    protected IlikeExpression(
            String propertyName,
            String value,
            Character escapeChar) {
        super(propertyName, value, escapeChar);
    }

    @Override
    protected String lhs(Dialect dialect, String column) {
        return dialect.getLowercaseFunction() + '(' + column + ')';
    }

    @Override
    protected String typedValue(String value) {
        return super.typedValue(value).toLowerCase();
    }

}


在此之后,剩下的唯一一件事就是让Restrictions使用这些新类:

public static Criterion like(String propertyName, Object value) {
    return new LikeExpression(propertyName, value);
}

public static Criterion like(String propertyName, String value, MatchMode matchMode) {
    return new LikeExpression(propertyName, value, matchMode);
}

public static Criterion like(String propertyName, String value, Character escapeChar) {
    return new LikeExpression(propertyName, value, escapeChar);
}

public static Criterion ilike(String propertyName, Object value) {
    return new IlikeExpression(propertyName, value);
}

public static Criterion ilike(String propertyName, String value, MatchMode matchMode) {
    return new IlikeExpression(propertyName, value, matchMode);
}

public static Criterion ilike(String propertyName, String value, Character escapeChar) {
    return new IlikeExpression(propertyName, value, escapeChar);
}


编辑:哦,是的。这适用于甲骨文。我们不确定其他数据库。

u4dcyp6a

u4dcyp6a2#

这不是一个非常干净的方法,但sqlRestrinction应该更容易:

criterions.add(Restrictions.sqlRestriction(columnName+ " ilike '!%' escape '!'"));

字符串
您甚至可以使用相同的原则进行搜索:

criterions.add(Restrictions.sqlRestriction(columnName+ " ilike '!%%' escape '!'"));

pvabu6sv

pvabu6sv3#

如果你直接使用LikeExpression,它允许你指定转义字符。我想这应该是你所需要的。

qv7cva1a

qv7cva1a4#

如果你使用Hibernate 3.2+,你可以子类化LikeExpression,然后创建工厂like/ilike方法:

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LikeExpression;
import org.hibernate.criterion.MatchMode;

public class EscapedLikeRestrictions {
    private EscapedLikeRestrictions() {}

    public static Criterion likeEscaped(String propertyName, String value, MatchMode matchMode) {
        return likeEscaped(propertyName, value, matchMode, false);
    }

    public static Criterion ilikeEscaped(String propertyName, String value, MatchMode matchMode) {
        return likeEscaped(propertyName, value, matchMode, true);
    }

    private static Criterion likeEscaped(String propertyName, String value, MatchMode matchMode, boolean ignoreCase) {
        return new LikeExpression(propertyName, escape(value), matchMode, '!', ignoreCase) {/*a trick to call protected constructor*/};
    }

    private static String escape(String value) {
        return value
                .replace("!", "!!")
                .replace("%", "!%")
                .replace("_", "!_");
    }
}

字符串

z5btuh9x

z5btuh9x5#

如果使用sqlRectritions,正确的方法如下:
第一个月
就像一个sql查询,不像=> use LIKE使用Oracle 12i。

相关问题