在除錯器中,我可以看到注釋以某種方式起作用。但是永遠不會呼叫方法initialize(AuthorConstraint)和isValid(Author, ConstraintValidatorContext)類。AuthorConstraintValidator
我搜索了好幾個小時的解決方案。但我不知道缺少什么。
包:hibernate-core 5.6.5.Final hibernate-validator 6.2.3.Final
注釋@AuthorConstraint
package myapp.entity.author.constraint;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* An annotation for special constraints of authors.
*/
@Constraint(validatedBy = {AuthorConstraintValidator.class})
@Retention(RUNTIME)
@Target({TYPE})
public @interface AuthorConstraint {
String message() default "An author needs either a first name or a last name.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
約束驗證器實作
package myapp.entity.author.constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import myapp.entity.author.Author;
/**
* A validator for special constraints of authors.
*/
public class AuthorConstraintValidator implements ConstraintValidator<AuthorConstraint, Author> {
/**
* {@inheritDoc}
*/
@Override
public void initialize(AuthorConstraint constraintAnnotation) {
}
@Override
public boolean isValid(Author author, ConstraintValidatorContext constraintValidatorContext) {
if (author == null) {
return true;
}
return author.getFirstName() != null || author.getLastName() != null;
}
}
作者類
package myapp.entity.author;
import myapp.data.DatedEntity;
import myapp.entity.author.constraint.AuthorConstraint;
import javax.persistence.*;
import java.io.Serializable;
/**
* Represents an author.
*/
@AuthorConstraint
@Entity
@Table(name = "author")
public class Author extends DatedEntity implements Serializable {
/**
* ID associated with an author.
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* First name of an author.
*/
@Column(name = "first_name")
private String firstName;
/**
* Last name of an author.
*/
@Column(name = "last_name")
private String lastName;
/**
* Returns the ID associated with an author.
*
* @return Primary key of an author.
*/
public Integer getId() {
return id;
}
/**
* Sets the ID of an author.
*
* @param id the ID of an author
*/
@SuppressWarnings({"unused", "SameParameterValue"})
void setId(Integer id) {
this.id = id;
}
/**
* Returns the first name of an author.
*
* @return First name of an author.
*/
@SuppressWarnings("unused")
public String getFirstName() {
return firstName;
}
/**
* Sets the first name of an author.
*
* @param firstName first name
*/
@SuppressWarnings("unused")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Returns the last name of an author.
*
* @return Last name of an author.
*/
@SuppressWarnings("unused")
public String getLastName() {
return lastName;
}
/**
* Sets the last name of an author.
*
* @param lastName last name
*/
@SuppressWarnings("unused")
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
驗證器的測驗代碼
當我自己創建一個驗證器物件時,我可以看到它按預期作業。
Author author = new Author();
AuthorConstraintValidator validator = new AuthorConstraintValidator();
validator.initialize(new AuthorConstraint() {
@Override
public Class<? extends Annotation> annotationType() {
return AuthorConstraint.class;
}
@Override
public String message() {
return null;
}
@Override
public Class<?>[] groups() {
return new Class[0];
}
@Override
public Class<? extends Payload>[] payload() {
return new Class[0];
}
});
System.err.println(validator.isValid(author, null));
// result: false
uj5u.com熱心網友回復:
您正在混合支持javax.*注釋的組件和支持注釋的組件jakarta.*。
如果你正在使用javax.persistence,你應該使用javax.validationHibernate Validator 6.2.x。它仍然受支持,并具有與 HV 7.0.x(針對 Jakarta EE 9 和jakarta.validation包)和即將推出的 HV 8.0.x(針對 Jakarta EE 10)相同的功能。
所以切換到 Hibernate Validator 6.2 和 Bean Validation 2.0。
您還應該檢查您的 EL 實作版本。它應該是:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>3.0.3</version>
</dependency>
確保您周圍沒有另一個,因為它們有很多不同的名稱。
然后如果它仍然不起作用,一件事是默認情況下 Hibernate ORM 在嘗試初始化 Hibernate Validator 時不會告訴您是否出現問題。
您可以通過將以下屬性設定為CALLBACK:
javax.persistence.validation.group.pre-persistjavax.persistence.validation.group.pre-updatejavax.persistence.validation.group.pre-remove
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450052.html
