简体   繁体   English

Javax.package 或替代方案中的 Hibernate @NotEmpty 注解等效

[英]Hibernate @NotEmpty annotation equivalent in Javax.package or alternative

Is there a way to implement @NotEmpty hibernate validation without writing custom validation?有没有办法在不编写自定义验证的情况下实现@NotEmpty休眠验证? javax.validation package does not contain this annotation. javax.validation 包不包含此注释。 Only @NotNull .只有@NotNull But it does not validate for Non-null but empty values.但它不会验证非空但空值。 So I would like to see an alternative for @NotEmpty .所以我想看到@NotEmpty的替代@NotEmpty

Using @Pattern ?使用@Pattern吗? How?如何?

NotEmpty 只是@NotNull@Size(min=1)

Please be aware that @NotEmpty will return valid for a List<> containing a null element.请注意,@NotEmpty 将对包含空元素的 List<> 返回有效。

Kind of bizarre in the case of a @QueryParam List<>在@QueryParam List<> 的情况下有点奇怪

As say Affe, I did a custom annotation, itself annotated with @NotNull and @Size(min=1) with a custom validator that iterates the collection and positions a boolean flag only if the elements are not null.正如 Affe 所说,我做了一个自定义注释,它本身用 @NotNull 和 @Size(min=1) 注释,并带有一个自定义验证器,该验证器仅在元素不为空时才迭代集合并定位一个布尔标志。

In the Hibernate @NotEmpty source code after Hibernate 6, it told us use the standard javax.validation.constraints.NotEmpty constraint instead:在 Hibernate 6 之后的 Hibernate @NotEmpty源代码中,它告诉我们改用标准的javax.validation.constraints.NotEmpty约束:

/**
 * Asserts that the annotated string, collection, map or array is not {@code null} or empty.
 *
 * @author Emmanuel Bernard
 * @author Hardy Ferentschik
 *
 * @deprecated use the standard {@link javax.validation.constraints.NotEmpty} constraint instead
 */

See: https://github.com/hibernate/hibernate-validator/blob/6.0/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java请参阅: https : //github.com/hibernate/hibernate-validator/blob/6.0/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java

This new annotation comes from Bean Validation 2.0 (JSR 380).这个新注释来自 Bean Validation 2.0 (JSR 380)。 See:看:

For Hibernate it is deprecated in the newer version.对于 Hibernate,它在较新的版本中已被弃用。
With the newer version of Javax validation it has @Empty使用较新版本的 Javax 验证,它具有@Empty

Use使用

import javax.validation.constraints.NotEmpty;

@NotEmpty
private List<Record> records;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM