Uploaded image for project: 'Forge'
  1. Forge
  2. FORGE-2490

Handling of unique constraint exceptions

    XMLWordPrintable

Details

    • Feature Request
    • Resolution: Unresolved
    • Major
    • 3.x Future
    • 2.19.2.Final
    • Scaffold
    • None

    Description

      If you scaffold an Entity in which you have an unique constraint (i.e: java-add-annotation --onProperty testField --annotation Column(unique=true)) the generated code isn't able to handle the exception when you violate the unique, unlike other restrictions, and redirects the user to the general error page, losing the inserted data.

      Reading about this my conclusion is that JPA doesn't manage this and delegates to the database. Anyway, It would be great if the generated Forge beans could handle the restriction just like with the others and show a validation error instead of redirecting to the error page.

      I have tried with two different approaches to manage this:
      1) Flush after the persist/merge in the bean so we can catch the SQLIntegrityConstraintViolationException in the update method and then add a proper message.
      2) Launch a previous select query to the database to see if the value is already in the database (not very efficient / smart)

      Following the option 1 we faced another problem and we have finally extended ours beans from a "general bean" in which we have an interceptor method with @AroundInvoke annotation to handle errors from the database. This appear to be necessary sometimes when we get an EJBTransactionRolledbackException (i.e when you are editing [not creating] an object, because the exception is launched in CMTTxInterceptor so we cannot catch it in the update method)

      Attachments

        Activity

          People

            Unassigned Unassigned
            aitorfcj Aitor Fdez-Ceballos (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: