【问题标题】:Using hibernate criteria, is there a way to escape special characters?使用休眠标准,有没有办法逃避特殊字符?
【发布时间】:2010-10-14 23:34:59
【问题描述】:

对于这个问题,我们希望避免编写特殊查询,因为查询在多个数据库中必须不同。仅使用休眠条件,我们希望能够转义特殊字符。

这种情况是需要特殊字符转义能力的原因:

假设我们在数据库中有表 'foo'。表 'foo' 仅包含 1 个字段,称为 '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 转义某些字符,或者创建一个不特定于数据库类型的解决方法?

【问题讨论】:

    标签: java database hibernate criteria escaping


    【解决方案1】:

    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);
    }
    

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

    【讨论】:

    • 嗨,我知道这是一个很老的帖子。如果可能的话,你介意发布完整的例子吗。
    【解决方案2】:

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

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

    您甚至可以使用相同的原则从搜索开始:

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

    【讨论】:

    • 我从这个解决方案开始,它在简单查询上对我来说效果很好,但后来遇到了一个问题——如果你需要在columnName 中使用{alias} 占位符,这个占位符总是指的是根实体映射到的表。如果您在查询中使用任何连接,Hibernate 无法插入连接表的别名。在这种情况下,我不得不求助于子类化LikeExpression
    • 这有帮助:我尝试了 criteria.add(Restrictions.sqlRestriction(columnName+ " like '!%' escape '!'"));我喜欢不适合我
    【解决方案3】:

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

    【讨论】:

    • 谢谢。希望我今天晚些时候能有时间尝试一下。等我试试再更新。
    • IlikeExpression 没有它。
    • LikeExpression 的所有构造函数都受到保护,您需要子类化并创建一个公共构造函数。
    【解决方案4】:

    如果你使用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("_", "!_");
        }
    }
    

    【讨论】:

      【解决方案5】:

      如果你使用sqlRestrictions,正确的做法如下:

      criterions.add(Restrictions.sqlRestriction(columnName+" LIKE '!%' ESCAPE '!'"));

      就像一个 sql 查询,不起作用 ilike => 在 Oracle 12i 中使用 LIKE。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 1970-01-01
        • 2021-08-27
        相关资源
        最近更新 更多