title: How to catch PSQLException value too long for type character varying
body: <p>I'm currently testing the data access layer that I've created in spring (PersistenceContext is injected). So I have a stateless EJB that calls a service for example UserService, that inserts/delete/update data in the database.</p>
  
  <p>The service works fine, I was able to insert database. But when I was testing and I input string value that is longer than the set length I got:</p>
  
  <pre><code>javax.transaction.RollbackException: Transaction marked for rollback.
  WARNING: DTX5014: Caught exception in beforeCompletion() callback:
  javax.persistence.PersistenceException: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
  Caused by: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
  Caused by: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
  </code></pre>
  
  <p>My partial code:</p>
  
  <pre><code>@PersistenceContext
  protected EntityManager entityManager;
  
  try {
      entityManager.persist(e);
  } catch(Exception e) {
    //log message here
  }
  </code></pre>
  
  <p>Then I've tried everything to catch these errors but I was not able to. Any suggestion on how to resolve the issue? </p>
  
  <p>Thanks,
  <br />czetsuya</p>