hibernate - 如何使用 Hibernate 配置自定义消息插值?

标签 hibernate validation jpa bean-validation hibernate-validator

我编写自定义消息插值。我希望 JPA 将使用我的自定义消息插值。这里http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-bootstrapping.html#section-message-interpolator我找到了以下描述:

Configuration<?> configuration = Validation.byDefaultProvider().configure();
ValidatorFactory factory = configuration
    .messageInterpolator(new ValueFormatterMessageInterpolator(configuration.getDefaultMessageInterpolator()))
    .buildValidatorFactory();

Validator validator = factory.getValidator();

但是我应该在哪里写这样的代码?在 init-servlet 中的 web.xml 中?我可以在persistance.xml 中提供这样的代码吗?

附言我复制并粘贴代码。在我的情况下
ValueFormatterMessageInterpolator(configuration.getDefaultMessageInterpolator()))

将在这样的事情上发生变化
CustomMessageInterpolator(configuration.getDefaultMessageInterpolator()))

另见 How do I dynamically resolve message parameters with Hibernate Validator?

最佳答案

JSR-303 bean 验证框架提供了通过 XML 配置验证框架的可能性。

例如。 META-INF/validation.xml
见章节

4.4.6. XML Configuration: META-INF/validation.xml



详细信息:http://download.oracle.com/otndocs/jcp/bean_validation-1.0-fr-oth-JSpec/

4.4.6. XML 配置:META-INF/validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
    <default-provider>com.acme.ACMEProvider</default-provider>
    <message-interpolator>com.acme.ACMEAwareMessageInterpolator</message-interpolator>
    <constraint-mapping>META-INF/validation/order-constraints.xml</constraint-mapping>
    <constraint-mapping>META-INF/validation/catalog-constraints.xml</constraint-mapping>
    <constraint-mapping>META-INF/validation/customer-constraints.xml</constraint-mapping>
    <property name="com.acme.validation.logging">WARN</property>
    <property name="com.acme.validation.safetyChecking">failOnError</property>
</validation-config>

用你的持久性 jar (META-INF/validation.xml) 打包 xml 文件,它应该可以工作。

根据您的部署包(例如 EAR),可能需要将它放在 EAR 的 lib 文件夹中的共享库中。

hibernate 文档说:

The key to enable XML configuration for Hibernate Validator is the file validation.xml. If this file exists in the classpath its configuration will be applied when the ValidationFactory gets created. Example 4.1, “validation-configuration-1.0.xsd” shows a model view of the xsd valiation.xml has to adhere to.



http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#d0e1867

关于hibernate - 如何使用 Hibernate 配置自定义消息插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329803/

相关文章:

asp.net - 仅当满足页面中的所有验证时如何触发 DropDownList AutoPostBack

ios - CupertinoTextField 中的 Flutter 文本验证

java - 使用实体管理器进行 SQL 查询

java - 2 个 JPA 实体在同一张表上

java - 如何设置 JPA 项目与 Hibernate 一起使用?

java - 尝试使用 Hibernate 和 PostgreSQL 执行存储过程时出错

java - Hibernate mysql group by day连续结果

java - Tomcat 找不到 H2 内存数据库

c# - ASP.NET MultiLine TextBox - 带换行符 (\r\n) 的 MaxLength 的正则表达式验证器 - 客户端和服务器端验证的差异

java - JPA - 带有总结果数的动态查询