gpt4 book ai didi

java - jpa 验证分离对象

转载 作者:行者123 更新时间:2023-12-01 15:08:57 26 4
gpt4 key购买 nike

我正在尝试使用 jpa 验证来验证新的 bean 对象。

我收到此错误:

2012-09-24 11:19:55,584 WARN  [AbstractModel:32] The object is not persisted yet, this operation requires the object to already be persisted.
java.lang.Exception
at org.oscarehr.common.model.AbstractModel.hashCode(AbstractModel.java:32)
at org.hibernate.validator.engine.ConstraintViolationImpl.hashCode(ConstraintViolationImpl.java:127)
at java.util.HashMap.put(HashMap.java:389)
at java.util.HashSet.add(HashSet.java:217)
at java.util.AbstractCollection.addAll(AbstractCollection.java:322)
at java.util.HashSet.<init>(HashSet.java:117)
at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:120)
at org.apache.jsp.billing.CA.ON.reports.billingONNewReport_jsp.saveSubmittedBills(billingONNewReport_jsp.java:383)
at org.apache.jsp.billing.CA.ON.reports.billingONNewReport_jsp._jspService(billingONNewReport_jsp.java:796)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)
at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:680)
at org.apache.jsp.billing.CA.ON.billingONReport_jsp._jspService(billingONReport_jsp.java:109)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
....

我正在使用:

hibernate-entitymanager 3.4.0.GA
hibernate-validator 4.1.0.Final
validation-api 1.0.0.GA

我这样调用我的验证代码:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<BillingClaimHeader1>> constraintViolations = validator.validate(newBill);
MiscUtils.getLogger().info("num violations: " + constraintViolations.size());

其中 newBill 是类 BillingClaimHeader1 的新实例。

这是 BillingClaimHeader1 的类定义:

public class BillingClaimHeader1 extends AbstractModel<Integer> implements Serializable

我对整个 JPA/验证事情还很陌生,但我没有意识到我必须在验证对象之前保留它......似乎有点违反直觉......

感谢任何帮助!

干杯

贾勒特

最佳答案

请注意

  1. 此消息来自 org.oscarehr.common.model.AbstractModel,而不是 JPA
  2. 这是警告,而不是错误

实际上,在您的情况下,您可以安全地忽略此消息。

org.oscarehr.common.model.AbstractModel 使用实体标识符来实现 equals()hashCode()。这种方法在与生成的标识符一起使用时有一个缺点 - 当您保存对象时,它会破坏 equals()hashCode() 的约定,即 hashCode() transient 对象的 与保存后同一对象的 hashCode() 不同。

为了通知您可能出现的问题,org.oscarehr.common.model.AbstractModel 的作者决定在您对 transient 对象调用 hashCode() 时发出警告。但是,在您的情况下,您可以安全地忽略此警告,因为 hashCode() 由仅对 transient 对象进行操作的验证例程在内部调用(据我所知)。

另请参阅:

关于java - jpa 验证分离对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12568211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com