我如何指定一个Hibernate" @ Pattern"使用.properties文件或数据库中的正则表达式进行注释 [英] How would I specify a Hibernate "@Pattern" annotation using a regular expression from a .properties file or database

查看:867
本文介绍了我如何指定一个Hibernate" @ Pattern"使用.properties文件或数据库中的正则表达式进行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情境:我想根据用户属性执行Hibernate验证(根据用户的帐户数据允许输入的不同验证规则) - 我认为必须可以使用.properties用于指定特定正则表达式的文件,但我无法弄清楚出现了什么问题:

Situation: I would like to perform Hibernate Validation based upon user properties (to allow different validation rules for input based upon a user's account data) - I think it must be possible to use a .properties file to specify a particular regular expression, but I can't figure out what is wrong:

我当前指定验证正则表达式的方法从一个常量中拉出该正则表达式特定的接口文件(将所有内容保存在一起)并将其作为常量插入每个变量的 @Pattern()注释中 - 例如变量 workPhone

My current method of specifying a validation regex pulls that regex from a constant in a particular interface file (to keep everything together) and plugs it in as a constant in an @Pattern() annotation for each variable - e.g. for the variable workPhone:

@Column(name = "WORK_PHONE")
@NotEmpty(message = "{ContactInfo.workPhone.notEmpty}")
@Pattern(regexp = PHONE_NUMBER_PATTERN_SL, message = "{ContactInfo.workPhone.regexp.msg}")
@Size(max = 10, message = "{ContactInfo.workPhone.size}")
protected String                workPhone;

...其中正则表达式存储在静态最终字符串 PHONE_NUMBER_PATTERN_SL 以及所有 {ContactInfo.workPhone ...} 来自.properties文件的调用:

...where the regex is stored in the static final String PHONE_NUMBER_PATTERN_SL and all the {ContactInfo.workPhone...} calls come from a .properties file:

ContactInfo.workPhone.notEmpty=Please enter your phone number.
ContactInfo.workPhone.regexp.msg=Invalid characters entered in phone. Use this format XXX-XXX-XXXX.
ContactInfo.workPhone.size=Phone can not be longer than 10 digits.

不幸的是,这种安排使得验证模式在应用程序范围内(编译),因为我无法想象为不同公司,位置,就业岗位等的不同用户更改它的方法。为了能够根据此信息进行区分,我还想将正则表达式存储在属性文件中,我尝试以这种方式包括:

Unfortunately, this arrangement makes the validation pattern application-wide (compiled), as I can't figure a way to change it for a different user in a different company, location, employment position, etc. To make it possible to differentiate based upon this info, I'd like to also store the regex in the properties file, and I try to include it this way:

ContactInfo.workPhone.regexp=\d{3}-\d{3}-\d{4}

同时在第一个代码清单中第三行的注释中包含引用:

while including the reference in the annotation from the third line in the first code listing:

@Pattern(regexp = "{ContactInfo.workPhone.regexp}", message = "{ContactInfo.workPhone.regexp.msg}")

然后我会为不同的场合切换属性文件,例如允许/需要非美国电话号码格式。

I would then switch out the properties files for different occasions, such as to allow/require a non-U.S. telephone number format.

问题:是否有可能做我想做的事情?有没有更好的方法来指定模式(甚至可能允许数据库调用而不是属性文件)?

Question: Is it possible to do what I want to do? Is there a better way to specify the pattern (which might allow even a database call instead of a properties file)?

此外,我不是最好的(因为我正在接替另一个开发人员),所以如果有人能够指出我关注使用@Pattern注释或其他Hibernate正则表达式验证标记的重点资源,那么这可能会给我所需的所有信息。

Additionally, I'm not the best at this (as I'm taking over from another developer), so if someone could merely point me to a focused resource concerning the use of the @Pattern annotation or other Hibernate regex validation markup, then that might give me all the info I need.

TL; DR :是否可以对Hibernate模式验证中使用的表达式使用动态设置或修改的值,而不是预定义和预编译常量?

TL;DR: Is it possible to use a dynamically-set or modified value for the expression used in Hibernate Pattern Validation rather than a predefined and precompiled constant?

推荐答案

在注释中,您只能引用常量表达式,因此从属性文件或数据库加载值不起作用这里。

Within annotations you can only refer to constant expressions, so loading values from a property file or database wouldn't work here.

您可以使用 API Hibernate Validator 4.2中引入的c约束声明,允许在运行时定义约束。您的示例可能如下所示:

You could use the API for dynamic constraint declaration introduced in Hibernate Validator 4.2 which allows to define constraints at runtime. Your example might look like that:

String dynamicPattern = ...;

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( ContactInfo.class )
    .property( "workPhone", FIELD )
    .constraint( new PatternDef().regexp( dynamicPattern ) );

HibernateValidatorConfiguration config = 
    Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );

Validator validator = config.buildValidatorFactory().getValidator();

这篇关于我如何指定一个Hibernate" @ Pattern"使用.properties文件或数据库中的正则表达式进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆