1

In my db I store telephone numbers of things as input by the user (I want to let user decide how he format their phone number)

when users search for a phone number they most likely wont format the number in a way that I can just compare the two strings. Even 'like' wont do the trick since maybe the number has parenthesis or some other usual phone separator in the middle.

I want to be able to compare each telephone on the DB with with my search criteria after applying a filter and comparing the results. So far the filter I want to apply is this one:

private String numberFilter(String num){
    num = num.replace("+", "00");
    num = num.replace("(", "");
    num = num.replace("(", "");
    num = num.replace("-", "");
    num = num.replace(" ", "");
    if (num.contains("ext"))
        num = num.substring(0, num.indexOf("ext"));
    return num;
}

And what I'm doing to compare the telephones (still missing the filter)

public List<Entity> getEntityByTelephone(String telephone) {
    DetachedCriteria criteria = DetachedCriteria.forClass(Entity.class);
    criteria.createAlias("telephones", "tl", CriteriaSpecification.INNER_JOIN);
    criteria.add(Restrictions.like("tl.number", telephone));
    return (List<Entity>) getHibernateTemplate().findByCriteria(criteria);
}

So help on a way to use the filter above as a comparator in hibernate would be appreciated

4

2 回答 2

2

我不知道如何准确地实现您想要的(我想知道性能),实际上,我会以不同的方式实现。

如果您真的想让用户输入他喜欢的电话号码,那么只需存储用户输入以及规范化版本并在规范化输入上执行搜索

对于规范化,我会使用Google 的库来处理电话号码,这似乎是完成这项工作的完美库。存储 aPhoneNumber可能需要实现 a UserType,但这没什么大不了的。

于 2010-06-23T20:00:12.963 回答
1

我建议您使用正则表达式来查询存储的数字。我还建议以标准格式存储数字,但我了解您希望保留用户最初提供的格式。

有关在 Hibernate 中使用不同的正则表达式查询的说明,请参见此处

您可能需要使用Restrictions.sqlRestriction(),因为 Hibernate 本身不支持Restrictions APIrlike中的运算符。

于 2010-06-23T19:58:58.663 回答