java - Gracefully handling ConstraintValidationExceptions in Hibernate -


There are too many terms on the SO. In hibernation, validity, catching exceptions, and other sites related to moving forward, however, everyone has kept me against some very specific design options in the situation described below. I feel like a normal situation I can not believe There is no clear solution.

  • I want to take advantage of the verification of the JavaVax (and hibernation) annotation,
  • I want to be right, or throwing out, invalid objects,
  • I want to take advantage of javax prepressors, etc. to replicate or take time-based actions on my items.

    Three concerts do not seem to work

    • An exception occurs when I have an illegal object at any time when I Obstacle violation exception will get I can not get official if constraintViolationException is included, or considered as a special case [1], though ...

    • I begin to be able to move forward if I hold ConstraintiolationException and move on to the next object, but Hibernate complains when I try to flush or reduce the transaction because

      • I thought about trying to empty the object or session.evict (MyObject) to successfully use the "brake" without knowing the object.
      • Before I try to save it (verification "manual") I can run - it is being done twice - which seems silly, but it is not @preparist hook Can not make a call, which could lead to a false violation.

        What does one do?

        To be solid, I have a number of (10,000+) objects, one of which is a small handful always invalidated.

          @Entiree public class applies myPojo Serializable {@ ID @ Generated Personal Long ID; @notbank private string other string; @ Not Knol @ Temporal (Temral Type: TIMESTAMP) Private DeadAct; @ Not Knol @ Temporal (Temral type.tmeampp) Pvt. Date updated; @ Not Knol @ Temporal (Temporal Type: Timesstamp) Private Date Post Date; @PrePersistPrePersist Protected on () {this.createdAt = New Date (); This.updatedAt = this.createdAt; If (this.postedDate == zero) this.postedDate = this.createdAt; }}   

        I want to save this function, and want to hold any object with an obligation violation:

          void saveResource (MyPojo myPojp) {Try {session.saveOrUpdate (mypojo); } Hold (Last ConstraintViolationException pre) {System.err.println ("" Can not save + + my PO + "because" + this.getValidationErrors (myPojo)); }}   

        Where getValidationErrors () returns the string of all Constraint violation s found by creating and using a verification factory manually.

        I know that only other possibilities are doing a transaction during each save (which seems like a bad idea because there are real exceptions, I do not want to continue any item) Or maybe writing a custom legal call pre-operative hook (maintenance nightmare!) Calls before making a manual call before saving me.

        [1]: I know that this is a helpless reference, but I am studying so much that I can not find it again. For any person, it should be happy to clarify in more technical details which errors that make the transaction void, are recoverable. "post-text" itemprop = "text">

          public interface PersistValidationGroup {} @NotBlank private string other string; @notalsal (group = {persistValidationGroup.class}) @ temporal (TemporalType.TIMESTAMP) Private DeadAct; @notalsal (group = {persist validation group.clash}) @ temporal (temparle type.Temststamp) pvt date updated; @notalsal (group = {persist validation group.clash}) @ temporal (temral typed timesstamp) private date post date;   

        Then, when you manually validate:

          validator.validate (object, javax.validation.groups.Default.class);   

        At this point, only those fields that have been marked with the default verification group (the way it is used if you leave the "group" attribute, then it will be valid ) Anything in the PersistValidationGroup will not be valid.

        However, I would have to be in hibernation in this way:

          validator.validate (object);   

        So all the groups will be valid.

Comments