title: Handling ConstraintviolationException in JPA
body: <p>My problem is very usual but i can't seem to find any solution. I have searched a lot but didn't find anything</p>
  
  <p><strong>Situation:</strong></p>
  
  <p>tried to insert a row which caused foreign key violation that caused my transaction to rollback.
  No matter what exception i handle it always throws 500 from my rest endpoint which is not acceptable at all</p>
  
  <p><strong>Question:</strong>
  How to handle this gracefully. Below is my code which is not working</p>
  
  <pre><code>@Transactional(dontRollbackOn={InvalidModelException.class,PersistenceException.class,ConstraintViolationException.class,MySQLIntegrityConstraintViolationException.class})
      @Override   
      public PointAudit createPointAudit(PointAudit pointAudit) throws EmptyModelException, InvalidModelException {
          if(pointAudit != null) {
              try {
                  this.entityManager.persist(pointAudit);
                  this.entityManager.flush();
              }
  
              catch(RollbackException x) {
                  LOGGER.error(x.getMessage(), x);
              }
              catch(PersistenceException x) {
                  if(x.getCause() instanceof ConstraintViolationException) {
                      if(x.getCause().getCause() instanceof MySQLIntegrityConstraintViolationException)
                      {
                          MySQLIntegrityConstraintViolationException e = (MySQLIntegrityConstraintViolationException)x.getCause().getCause();
                          if(e.getMessage().contains("USER_ID")) {
                              throw new InvalidModelException("Invalid user is provided");
                          }
                          else {
                              if(e.getMessage().contains("STATUS")) {
                                  throw new InvalidModelException("Invalid status is provided");
                              }
                              else {
                                  if(e.getMessage().contains("CHANNEL")) {
                                      throw new InvalidModelException("Invalid channel is provided");
                                  }
                                  else {
                                      if(e.getMessage().contains("ACTION")) {
                                          throw new InvalidModelException("Invalid action is provided");
                                      }
                                  }
                              }
                          }
                      }
  
                  }
                  else {
                      while( !(x.getCause() instanceof RollbackException) || x.getCause() == null) {
                          LOGGER.error(x.getMessage(), x);
                      }
                  }
              }
          }
          else {
              throw new EmptyModelException("Point Audit is empty");
          }
          return pointAudit;
      }
  </code></pre>
  
  <p>If any further code is required please let me know</p>