gpt4 book ai didi

java - 如何通过 hibernate 验证组件实体的属性?

转载 作者:行者123 更新时间:2023-12-01 13:18:01 28 4
gpt4 key购买 nike

我有以下类(class):

class Foo
{
@Valid
private Bar bar;
}

在 Bar,我有:

class Bar
{
@NotEmpty
private String baz;
}

我想验证 foo 的属性 bar.baz。但是,当我尝试 validateProperty() 并传递字符串 bar.baz 作为属性名称时,我收到一条错误消息,指出不存在这样的属性。难道不能以这种方式验证子属性吗?

最佳答案

根据 Bean Validation specification该属性必须是您正在验证的 bean 的直接属性:

Set> validateProperty(T object, String propertyName, Class... groups) validates a given field or property of an object. An IllegalArgumentException is thrown when validateProperty() is called and object is null or propertyName is null empty or invalid or null is passed to the varargs groups parameter. The property name is the JavaBeans property name (as defined by the JavaBeans Introspector class). This method implements the logic described in Section 4.6, “Validation routine” but only to the given property. @Valid is not honored by this method. This method is useful for partial object validation.

也就是说,Hibernate Validator 支持属性路径,您还可以看到 here 。为了进一步提供帮助,您需要发布完整的堆栈跟踪。

关于java - 如何通过 hibernate 验证组件实体的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311723/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com