javax.validation.ConstraintViolationException:preUpdate验证违反了Bean验证约束

问题描述

| 尝试使用JPA 2.0,SpringMvc 3.0以多对多关系插入新元素时,出现了令人讨厌的错误消息。 我有一张与国家的桌子,另一张与人的桌子。一个人可以链接到许多州,一个州可以链接到许多人。 在这种情况下,我有一个listOfStates,然后是一个人,我想将这些元素插入我的多对多关系中。 ManyToMany关系(表STATE)
    //bi-directional many-to-many association to Appointment
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(
name=\"PERSON_STATE\",joinColumns={
    @JoinColumn(name=\"PERSON_ID\",nullable=false)
    },inverseJoinColumns={
    @JoinColumn(name=\"CODE_STATE\",nullable=false)
    }
)
我从控制器调用的DAO代码
try{    
    getEntityManager().getTransaction().begin();            
    getEntityManager().persist(myPerson);                       

    IStateDAO stateDAO = new StateDAO();

    for (int i=0; i<listOfStates.length; i++){
        State myState = stateDAO.findState(listOfStates[i]);
        if (myState != null){                   
            myState.getPersons().add(myPerson);
            getEntityManager().persist(myState);
        }
    }

    getEntityManager().getTransaction().commit();           
    getEntityManager().close();         

} catch (RuntimeException re) {
    getEntityManager().close();
    throw re;           
}
有趣的是,当我不从网页插入数据时,此代码可以正常工作。我在这里做错了什么?我已经在数据库中有一些人和州。 全栈错误消息:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:\'preUpdate\'. Please refer to embedded ConstraintViolations for details.


javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:\'preUpdate\'. Please refer to embedded ConstraintViolations for details.
任何指针将不胜感激。预先谢谢大家。     

解决方法

哇!得到它了!我不得不将persistence.xml中的验证模式从“自动”更改为“无”,这基本上告诉应用程序根本不使用bean验证。错误消息消失了,我的DAO正常运行了。     ,异常指出使用JSR 303 Bean验证,并且Hibernate配置为(Persistence.xml)在更新任何内容之前对其进行检查。 JSR 303 Bean验证是类似于以下的注释: javax.validation.constraints.NotNull javax.validation.constraints.Size     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...