JBoss Seam to the rescue!

One area that had annoyed me about RESTEasy is dealing with JAXB annotated Entity Beans that get returned from a Stateless Session Bean. Once the SLSB returns the entity, the transaction is committed but the object graph that will be needed for the XML representation may not be fully initialized, thus resulting in a LazyInitializationException from Hibernate. Since the JAXB marshaling code lives on the web tier, this is generally a problem when using Hibernate + an SLSB.

RESTEasy invokes an SLSB via the EJBResourceInvoker. As a work around, I had been starting a UserTransaction around the invoke() method. While this works, it’s not an ideal situation. Thankfully JBoss Seam offers an much more elegant solution to this issue. To get around this, it’s simply a matter of doing the following:

  • Set up the SeamListener & SeamServletFilter in your web.xml
  • Name a persistence context to Seam’s components.xml
  • In your SLSB, inject the persistence context using @In rather than @PersistenceContext

And just like that, Seam handles all of the Hibernate magic and RESTEasy works with no additional code. In fact, I have removed the UserTransaction code block in the EJBResourceInvoker as a result. A special thanks to Gavin King for answering my questions on the JBoss forums to help get this working right.


2 thoughts on “JBoss Seam to the rescue!

Comments are closed.