对回调事件执行自动 Bean 验证时违反了 Bean 验证约束:“prePersist”

发布于 2024-12-07 02:19:47 字数 822 浏览 0 评论 0原文

我在 Netbeans 7 中创建了一个 EJB 会话外观来保存我的实体。 我的保险和 RatePlan 类之间有一个多对一映射。

public class Insurance{
    @ManyToOne(optional=false) 
    @JoinColumn(name="PLAN_ID")
    private RatePlan plan;
}
public class RatePlan{
    @OneToMany(mappedBy="plan")
    private Set<Insurance> insuranceItems;
}

当我尝试使用 EJB 会话 Bean 保存在数据库中时,遇到以下错误。

原因:javax.validation.ConstraintViolationException:在回调事件“prePersist”上执行自动 Bean 验证时,违反了 Bean 验证约束。有关详细信息,请参阅嵌入的 ConstraintViolations。

我所做的就是关闭 Persistence.xml 文件中的 Bean 验证。 我想知道发生了什么 Bean 验证错误,但我不知道如何或在哪里找到它,也不知道如何配置和捕获它。

我的 EJB 外观是一个像这样的简单类。

public class InsuranceFacade{
    public void saveInsurance(Insurance insurance){
        em.persist(insurance);
    }
}

有什么提示吗?

I created an EJB Session facade in my Netbeans 7 for saving my entity.
I have a manytoone mapping between my Insurance and RatePlan Class.

public class Insurance{
    @ManyToOne(optional=false) 
    @JoinColumn(name="PLAN_ID")
    private RatePlan plan;
}
public class RatePlan{
    @OneToMany(mappedBy="plan")
    private Set<Insurance> insuranceItems;
}

When I tried saving in my database using my EJB Session Bean, I am encountering below error.

Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.

What I did was to turn off my Bean validation in my Persistence.xml file.
I would like to know what Bean validation error has occurred but I dont know how or where to find it or how to configure and catch it.

My EJB facade is a simple class like tis.

public class InsuranceFacade{
    public void saveInsurance(Insurance insurance){
        em.persist(insurance);
    }
}

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一片旧的回忆 2024-12-14 02:19:47

我想知道发生了什么 Bean 验证错误,但我不知道如何或在哪里找到它,也不知道如何配置和捕获它。

要了解发生了哪些特定的约束违规,您只需检查捕获的异常即可。 ConstraintViolationException.getConstraintViolations() 返回一组 ConstraintViolation,您可以对其进行迭代和检查。

I would like to know what Bean validation error has occurred but I dont know how or where to find it or how to configure and catch it.

To know what specific constraint violations have occurred, you could just inspect the exception caught. ConstraintViolationException.getConstraintViolations() returns a Set of ConstraintViolations which you can iterate and inspect.

黎歌 2024-12-14 02:19:47

我遇到了同样的问题,但是经过几个小时寻找答案,终于找到了......您应该编辑您的 AbstractFacade.java 类并添加此代码

public void create(T entity) {

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if(constraintViolations.size() > 0){
        Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
        while(iterator.hasNext()){
            ConstraintViolation<T> cv = iterator.next();
            System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());

            JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
        }
    }else{
        getEntityManager().persist(entity);
    }
}

现在这个方法将提醒您哪个属性以及验证失败的原因。
我希望这对你有用,就像对我一样。

I got the same problem, but after hours looking for the answer, Finally I Found it.... You should edit your AbstractFacade.java class and add this code

public void create(T entity) {

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if(constraintViolations.size() > 0){
        Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
        while(iterator.hasNext()){
            ConstraintViolation<T> cv = iterator.next();
            System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());

            JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
        }
    }else{
        getEntityManager().persist(entity);
    }
}

Now this method will alert you which property and why it fails the validation.
I hope this works for you, as it does for me.

平生欢 2024-12-14 02:19:47
catch (EJBException e) {
        @SuppressWarnings("ThrowableResultIgnored")
        Exception cause = e.getCausedByException();
        if (cause instanceof ConstraintViolationException) {
            @SuppressWarnings("ThrowableResultIgnored")
            ConstraintViolationException cve = (ConstraintViolationException) e.getCausedByException();
            for (Iterator<ConstraintViolation<?>> it = cve.getConstraintViolations().iterator(); it.hasNext();) {
                ConstraintViolation<? extends Object> v = it.next();
                System.err.println(v);
                System.err.println("==>>"+v.getMessage());
            }
        }
        Assert.fail("ejb exception");
    }
catch (EJBException e) {
        @SuppressWarnings("ThrowableResultIgnored")
        Exception cause = e.getCausedByException();
        if (cause instanceof ConstraintViolationException) {
            @SuppressWarnings("ThrowableResultIgnored")
            ConstraintViolationException cve = (ConstraintViolationException) e.getCausedByException();
            for (Iterator<ConstraintViolation<?>> it = cve.getConstraintViolations().iterator(); it.hasNext();) {
                ConstraintViolation<? extends Object> v = it.next();
                System.err.println(v);
                System.err.println("==>>"+v.getMessage());
            }
        }
        Assert.fail("ejb exception");
    }
丑丑阿 2024-12-14 02:19:47

在持久化实体时捕获以下异常。就我而言,它位于 EJB add 方法中。我正在做em.persist()。然后检查服务器日志,您将看到哪个属性存在约束违规。

catch (ConstraintViolationException e) {
       log.log(Level.SEVERE,"Exception: ");
       e.getConstraintViolations().forEach(err->log.log(Level.SEVERE,err.toString()));
    }

Catch the following exception where you persisting the entity. In my case its in the EJB add method. where I am doing em.persist(). Then check the server log, you will see which attribute having constrain violation.

catch (ConstraintViolationException e) {
       log.log(Level.SEVERE,"Exception: ");
       e.getConstraintViolations().forEach(err->log.log(Level.SEVERE,err.toString()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文