-
1. Re: EntityManager and exceptions
Tomasz Zab?ocki May 28, 2006 6:07 PM (in response to Tomasz Zab?ocki)Ok maybe I'll specify the question in a different way:
How can I use a non-injected entity manager? -
2. Re: EntityManager and exceptions
Ana Oleski Jun 2, 2006 10:16 AM (in response to Tomasz Zab?ocki)Hi,
you could try the flush() method in the entityManager. It sends the sql to the database right away, that should generate your exception.
Note that this has no influence on commit, you can still use container-managed transactions.
Ana -
3. Re: EntityManager and exceptions
Lewis Gass Jun 5, 2006 6:24 PM (in response to Tomasz Zab?ocki)Whether your EntityManager is injected or not has nothing to do with your ability to catch an exception.
You can do this:try { em.persist(user); } catch (Throwable t) { if (t instanceof TheExceptionIAmWantToHandleLaterException) { ... } else { ... } }
-
4. Re: EntityManager and exceptions
Lewis Gass Jun 5, 2006 6:26 PM (in response to Tomasz Zab?ocki)Sorry forgot to include this (this would go in the implementation class):
public void createUser (...) { try { this.createUserImpl(...); } catch { ... } } private void createUserImpl (...) { em.persist(user); }
-
5. Re: EntityManager and exceptions
J-C jc Jun 6, 2006 3:17 AM (in response to Tomasz Zab?ocki)In your createUser method you could catch the exception and throw a new Exception (with the annotation ApplicationException with the rollback set to false).
Exception handling is describe in chapter 14 of the EJB core specification