栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用hibernate标准,是否可以转义特殊字符?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用hibernate标准,是否可以转义特殊字符?

Likeexpression的构造函数均受保护,因此这不是一个可行的选择。同样,它也有自己的问题。

我和一位同事创建了一个效果很好的补丁。补丁的要点是,对于使用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);}

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号