对回调事件执行自动 Bean 验证时违反了 Bean 验证约束:“prePersist”
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要了解发生了哪些特定的约束违规,您只需检查捕获的异常即可。 ConstraintViolationException.getConstraintViolations() 返回一组 ConstraintViolation,您可以对其进行迭代和检查。
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.
我遇到了同样的问题,但是经过几个小时寻找答案,终于找到了......您应该编辑您的 AbstractFacade.java 类并添加此代码
现在这个方法将提醒您哪个属性以及验证失败的原因。
我希望这对你有用,就像对我一样。
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
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.
在持久化实体时捕获以下异常。就我而言,它位于 EJB add 方法中。我正在做
em.persist()
。然后检查服务器日志,您将看到哪个属性存在约束违规。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.