Uploaded image for project: 'Application Server 7'
  1. Application Server 7
  2. AS7-4576

No ConstraintViolationException thrown by EntityManager persist upon pre-persist constraint violation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: Major Major
    • None
    • 7.1.1.Final
    • JPA / Hibernate
    • None
    • Hide

      1. deploy the attached modified kitchensink example
      2. go to http://127.0.0.1:8080/jboss-as-kitchensink
      3. click register
      4. notice the log

      15:31:15,065 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: before em.persist()
      15:31:15,066 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: prePersist
      15:31:15,068 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: after em.persist()
      15:31:15,074 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: Constraint violation!

      The expected correct log would not contain " XXX: after em.persist()"

      Show
      1. deploy the attached modified kitchensink example 2. go to http://127.0.0.1:8080/jboss-as-kitchensink 3. click register 4. notice the log 15:31:15,065 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: before em.persist() 15:31:15,066 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: prePersist 15:31:15,068 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: after em.persist() 15:31:15,074 INFO [stdout] (http-dhcp-26-127.brq.redhat.com-127.0.0.1-8080-2) XXX: Constraint violation! The expected correct log would not contain " XXX: after em.persist()"

      Per JPA 2.0 spec 3.6.1.2 Requirements for Automatic Validation upon Lifecycle Events, the "persistence provider must throw the javax.validation.ConstraintViolationException" ... and the validation must occur "immediately after the point" where callback method invocations, such as PrePersist have been completed.

      Having a Member entity with some @NonNull validator present on one of its fields, the following test shows the javax.validation.ConstraintViolationException is not thrown while invoking persist, but later inside flush():

            try {
               System.out.println("XXX: before em.persist()");
               em.persist(new Member());
               System.out.println("XXX: after em.persist()");
               em.flush();
               System.out.println("XXX: after em.flush()");
            }
            catch(javax.validation.ConstraintViolationException x) {
               System.out.println("XXX: Constraint violation!");
            }
      
      @Entity
      @XmlRootElement
      @Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
      public class Member implements Serializable {
      ...
         @NotNull
         @NotEmpty
         @Email
         private String email;
      ...
      
         @PrePersist
         public void prePersist() {
            System.out.println("XXX: prePersist");
         }
      }
      

            smarlow1@redhat.com Scott Marlow
            maschmid@redhat.com Marek Schmidt
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: