0 Replies Latest reply on Jun 24, 2007 2:12 PM by slugounet

    How to catch Entity Manager exceptions ?

    slugounet

      Hi !

      I have a few entity beans with validation annotations, and I'd like to be able to catch the exceptions thrown by the hibernate validator when something goes wrong.

      @Entity
      public class Country {
      
       @Id
       @GeneratedValue( strategy=GenerationType.AUTO )
       private Integer id;
      
       @NotNull
       @Length(min = 1, max = 255)
       private String name;
      }
      


      @Stateless
      @Remote(Update.class)
      public class UpdateSessionBean implements Serializable, Update {
       public CountryDataBean updateCountry( CountryDataBean bean )
       throws UpdateException {
       Country entity = this.entityManager.find( Country.class, bean.getId() );
       if ( entity == null ) {
       throw new UpdateException( "Country not found" );
       }
      
       CountryMapper mapper = new CountryMapper();
       mapper.toEntity( bean, entity );
      
       try {
       entity = this.entityManager.merge( entity );
       }
       catch( InvalidStateException e ) {
       ... do some stuff
       }
      
       mapper.toDataBean( entity, bean );
       return bean;
       }
      }
      


      When I force an error, I get that kind of error message :

      19:49:43,158 WARN [arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@7cd469
      javax.persistence.PersistenceException: org.hibernate.validator.InvalidStateException: validation failed for: prixmac.indexation.database.Country
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:527)
       at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:114)
       at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:249)
       at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:88)
       at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
       at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1256)
       at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:135)
       at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:87)
       at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      ...
      


      Does anyone have an idea of how I could catch those damned exceptions in my session bean ? Cause it's pretty pointless for me to validate the data if I can't find a way to know that something went wrong :(

      I'm using JBoss 4.2.0 !

      Best regards,

      Guillaume