2 Replies Latest reply on Sep 12, 2008 10:22 AM by schlegel

    Unique Constraint and transaction problem

    fvalente

      I'm facing a problem when I use the unique constraint in one column (acronym) of a table.


      I added the attribute unique="true" in my orm.xml file.


      Here is part of the code of my EntityHome


      @Override
      @Transactional
      public String persist()
      {
          String result = "";
                
          try
          {
              getInstance().setStatus(OU.Status.DRAFT);
              result = super.persist();
          }
          catch (EntityExistsException e)
          {
              addFacesMessageFromResourceBundle("ou.message.ouSameAcronym",getInstance().getAcronym());
          }
      
          return result;
      }
      
      @Override
      @Transactional
      public String update()
      {
          String result = "";
                
          try
          {
              result = super.update();
          }
          catch (EntityExistsException e)
          {
              addFacesMessageFromResourceBundle("ou.message.ouSameAcronym",getInstance().getAcronym());
          }
      
          return result;
      }
      



      When I try to create a new instance with the same acronym I can see (on the console) that Seam throws ConstraintViolationException and BatchUpdateException. I return to the page with the form. I see the message that I added (it was also showing "Transacion Failed", but I removed it from the messages file), but when I change the acronym and click the button to save I get Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist.


      I think that Seam closes the transaction when the first exception occurs.


      Can anyone help me?