Hibernate validation annotation - validate that at least one field is not null(Hibernate 验证注解 - 验证至少一个字段不为空)
问题描述
有没有办法使用定义的注释定义 Hibernate 验证规则 这里,说明至少有一个字段不能为空?
Is there a way to define a Hibernate validation rule using annotations as defined here, stating that at least one field shall be not null?
这将是一个假设示例(@OneFieldMustBeNotNullConstraint
并不真正存在):
This would be a hypothetical example (@OneFieldMustBeNotNullConstraint
does not really exist):
@Entity
@OneFieldMustBeNotNullConstraint(list={fieldA,fieldB})
public class Card {
@Id
@GeneratedValue
private Integer card_id;
@Column(nullable = true)
private Long fieldA;
@Column(nullable = true)
private Long fieldB;
}
在图示的情况下,fieldA 可以为 null 或 fieldB 可以为 null,但不能同时为 null.
In the illustrated case, fieldA can be null or fieldB can be null, but not both.
一种方法是创建我自己的验证器,但如果它已经存在,我想避免.如果您已经制作了一个验证器,请分享一个...谢谢!
One way would be to create my own validator, but I'd like to avoid if it already exists. Please share one validator if you have one already made... thanks!
推荐答案
我终于写出了整个验证器:
I finally wrote the whole validator:
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import org.apache.commons.beanutils.PropertyUtils;
@Target( { TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CheckAtLeastOneNotNull.CheckAtLeastOneNotNullValidator.class)
@Documented
public @interface CheckAtLeastOneNotNull {
String message() default "{com.xxx.constraints.checkatleastnotnull}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String[] fieldNames();
public static class CheckAtLeastOneNotNullValidator implements ConstraintValidator<CheckAtLeastOneNotNull, Object> {
private String[] fieldNames;
public void initialize(CheckAtLeastOneNotNull constraintAnnotation) {
this.fieldNames = constraintAnnotation.fieldNames();
}
public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {
if (object == null) {
return true;
}
try {
for (String fieldName:fieldNames){
Object property = PropertyUtils.getProperty(object, fieldName);
if (property != null) return true;
}
return false;
} catch (Exception e) {
return false;
}
}
}
}
使用示例:
@Entity
@CheckAtLeastOneNotNull(fieldNames={"fieldA","fieldB"})
public class Reward {
@Id
@GeneratedValue
private Integer id;
private Integer fieldA;
private Integer fieldB;
[...] // accessors, other fields, etc.
}
这篇关于Hibernate 验证注解 - 验证至少一个字段不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Hibernate 验证注解 - 验证至少一个字段不为空
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01