自定义 Bean 验证约束和对 persist() 的验证



REMEBER KIDS!!您应该检查变量是否为空;)。很抱歉有一个如此简单的解决方案的问题。脑滞是真的! 如果有人知道如何配置持久性.xml要在持久化(更新(之前检查验证,请发表评论,这个问题我还没有解决。

我正在尝试在休眠中创建自定义验证注释。可悲的是,在测试过程中(使用Validator和ValidatorFactory(,我得到了,可悲的是我不知道为什么:

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.

这是我的注释:

@Documented
@Constraint(validatedBy=PeselValidator.class)
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyPesel {
String message() default "{wrong.pesel.format}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class PeselValidator implements ConstraintValidator<MyPesel,String>{
@Override
public boolean isValid(String arg0, ConstraintValidatorContext arg1) {
IF(ARG0==NULL)
RETURN FALSE; //!!!!!!!!!!!!!!
//arg0.matches("^\d{4}[1-31]\d{5}$")
if(arg0.length()==11)  
return true;
else
return false;
}
}

这是我的作者代码:

@Entity
public class Author {
@Id
@GeneratedValue
private int Id;
@NotNull
@Size(min=5,max=30)
private String name;
@NotNull
@MyPesel
private String pesel;
@ManyToMany
@JoinColumn(name="book_id")
private Collection<Book> book= new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPesel() {
return pesel;
}
public void setPesel(String pesel) {
this.pesel = pesel;
}
}

并测试:

@Test
public void goodBookBadAuthor() {
Book book=new Book();
book.setTitle("quite good title");
book.getAuthor().add(new Author());
Author author=new Author();
author.setName("y");
author.setPesel("abc");
book.getAuthor().add(author);
Validator validator=validatorFactory.getValidator();
Set<ConstraintViolation<Book>> constraintViolations=validator.validate(book);
assertEquals(2,constraintViolations.size());
}

第二个问题是,即使我不使用测试也可以@Pesel但是当我想使用 entityManager 向数据库添加一些东西时(使用 persist(((,hibernate 不会检查 ma 验证。 我读到需要更改验证模式。我已经更改了它,但它不起作用。我真的希望你们能帮助我。有我的坚持.xml。

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="ormBook">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>bean.Book</class>
<class>bean.Author</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://127.0.0.1:3307/db_hibernate_3?useSSL=true&amp;serverTimezone=UTC&amp;logger=Slf4JLogger&amp;profileSQL=true" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="jbjb123" />
<property name="javax.persistence.validation.mode" value="AUTO"/>
<property name="javax.persistence.validation.group.pre-persist" value="javax.validation.groups.Default" />
<property name="javax.persistence.validation.group.pre-update" value="javax.validation.groups.Default" />
<property name="javax.persistence.validation.group.pre-remove" value=""/>       
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />

<property name="hibernate.connection.isolation" value="2" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.connection.pool_size" value="10" />
</properties>
</persistence-unit>
</persistence>

在这种情况下,我相信你可以使用@Pattern注释,我相信。

根据我的经验,编写自定义注释只会让事情变得更加复杂,而 hibernate 提供的验证对您可能需要的一切都有好处。

其他所有内容可能都应该在上面的层中处理。

最新更新