5 Replies Latest reply on Jun 6, 2006 3:17 AM by J-C jc

    EntityManager and exceptions

    Tomasz Zab?ocki Newbie

      Here is what I would like to do:

      This is my bussiness interface:

      package pl.com.filmservice.ejb;
      
      import java.sql.Date;
      import java.util.List;
      import pl.com.filmservice.par.*;
      
      public interface FilmService
      {
       ...
       public void createUser(User user);
       ...
      }


      and the implementing bean:

      package pl.com.filmservice.ejb;
      
      import java.sql.Date;
      import java.util.List;
      import java.util.ArrayList;
      import javax.ejb.*;
      import javax.persistence.*;
      import pl.com.filmservice.par.*;
      
      @Stateless
      public class FilmServiceBean implements FilmService
      {
       @PersistenceContext(unitName="FSWise")
       EntityManager em;
       ...
       public void createUser(User user)
       {
       em.persist(user);
       }
       ...
      }


      What I would like to do is to check if the em.persist(user) threw a pk constraint violation exception, catch it and throw my own exception class so that I can cathc it later on in the application.

      The problem is that the transaction ends when the createUser method ends and then the exception is thrown. I would like to catch it just after persist() invocation.

      I've tried to use EntityManager.createTransaction(), begin the transaction and commit it just after the persist, but then i get the illegalStateException (i can't call beginTransaction on the managed, injected EntityManager).
      Any ideas about how to solve the problem?