title: EJB and persistence exception
body: <p>By working with <strong>Java EE</strong>, I defined a <em>persistence layer</em> that exposes serval <strong>DAOs</strong> (extending <code>GenericDAO</code> and by using <strong>Hibernate</strong> as <em>ORM</em>).</p>
  
  <p>Then, I inject a certail DAO in order to work with it into another class.</p>
  
  <pre><code>@Inject
  private MyDAO myDAO;
  
  public void writeSomething(String key, String data) {
      try {
          myDAO.create(key, data);
      } catch (ConstraintViolationException e) {
          // BLOCK 1
      } catch (Exception e) {
          // BLOCK 2
      }
  }
  </code></pre>
  
  <p>Now, I'd like to test exceptions, by trying to write an entry duplication.
  As expected, the application returns an error, but it isn't a <code>ConstraintViolationException</code> instance, but something else (in fact, <code>EJBException</code>).</p>
  
  <pre><code>11:42:19,065 ERROR [org.jboss.ejb3.invocation] (http-localhost-127.0.0.1-8080-3) JBAS014134: EJB Invocation failed on component MyDAO for method public java.lang.Object my.app.dal.genericdao.GenericDAOImpl.create(java.lang.Object) throws java.lang.IllegalStateException,javax.persistence.PersistenceException,org.hibernate.exception.ConstraintViolationException: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry
  </code></pre>
  
  <p>How can I to catch a specific exception thrown by <code>myDAO</code> according to the situation (in this case a constraint violation)?</p>