EJB3/EntityManager/Transactions and LazyInitializationExcept
ngtdave Jun 6, 2006 8:32 PMSo I implemented an stateless session bean which in turn uses an entity manager. Under that is hibernate. I am once again trying to answer the age old question of how do I get around the LazyInitializationException.
The code in my struts action looks like this:
PetServiceInterface psi = null; try { InitialContext ctx = new InitialContext(); psi = (PetServiceInterface) ctx.lookup("PetService/local"); } catch (Exception e) { e.printStackTrace(); } Person customer = psi.findPerson(12); OwnerForm of = (OwnerForm) form; of.setName(customer.getName()); of.setAge(customer.getAge()); of.setPets(customer.getPets()); return (mapping.findForward("page"));
My ejb looks like this:
@Service(objectName = "whatever:service=PetService") @Stateless public class PetService implements PetServiceInterface { @PersistenceContext(unitName="pet") EntityManager em; public Person findPerson(int id) { return em.find(Person.class, Integer.valueOf(id)); }
Now in my jsp I iterate the pets on the customer which throws a LazyInitializationException because the session is already closed.
Looking at secion 4.1.1 in
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/#transactions
it says this:
This is the default EJB3 persistence model in a Java EE environment (JTA bounded, transaction-scoped persistence context); injected (or looked up) entity managers share the same persistence context for a particular JTA transaction. The beauty of EJB3 is that you don't have to care about that anymore and just see data access through entity manager and demaraction of transaction scope on session beans as completely orthogonal.
Since I'm running JBoss 4.0.4.GA.Patch1, and using EBJ3, I was hoping the container would take care of the transactions for me. In other words, I was hoping the servlet request then response would all be in a JTA transaction, and the EJB would inherit that transaction, and the EntityManager would then inherit the same one, and so on.
If I mark the ejb with:
@TransactionAttribute(TransactionAttributeType.MANDATORY)
I will get an exception because there is no transaction. So am I missunderstanding the quore about not needing to care about the transaction, or am I missing something in my setup to let the container manage it all for me?
Note: Everything else works in the code as long as I stay in the action. In other words, I get a customer, set the values, and the action returns without error as expected.
Before I was using a ServletFilter to manage the hibernate session, but I was hoping to get rid of that code.
Any help would be greatly appreciated.