jpa验证分离的对象

时间:2012-09-24 15:32:52

标签: java hibernate validation bean-validation

我正在尝试使用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 /验证事情都很陌生,但我没有意识到在验证它之前我必须坚持一个对象......看起来有点违反直觉......

我感谢任何帮助!

欢呼声

Jarrett的

2 个答案:

答案 0 :(得分:1)

请注意

  1. 此消息来自org.oscarehr.common.model.AbstractModel,而不是JPA
  2. 这是一个警告,而不是错误
  3. 实际上,您可以放心地忽略此消息。

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

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

    另见:

答案 1 :(得分:0)

您的实体与PersistenceContext分离。您必须为该实例执行manage state