I am working on creating RESTful versions of some existing services for a project. The current application uses Hibernate 3 with a jboss 6 server so any new services I add have to work within that environment. I decided to start with a simple GET request. I managed to get everything set up correctly and my service method is being run. The problem I'm running into is that I get 'lazy load' exceptions when Jackson tries to serialize the response to send it back to the client.
The two most common solutions I have see for this issue will not work here. Changing the fetch type to EAGER would potentially break my existing services. Adding annotations to ignore the fields causing the exception would also not work since those fields are needed by the client.
Normally, my solution to something like this would be to 'front-load' the collections need by the client during the service call. Unfortunately, that can only be done inside a transaction. By the time Jackson is ready to serialize my response, the service method has already returned and the transaction has been closed.
Does anyone know of a way to work around this issue? Is there some 'hook' that jackson provides that would let me do the serialization inside a transaction?
Update here is example of what my code looks like:
service provider-
@Path("service")
public interface ClientServiceProvider
{
@GET
@Path("web/brand/{id}")
@Produces(MediaType.APPLICATION_JSON)
public abstract Response getBrandWeb(@PathParam("id") Long brandId);
}
service bean-
@Stateless(name = "ClientServiceProvider")
@Local(ClientServiceProvider.class)
public class ClientServiceProviderBean implements ClientServiceProvider
{
@Override
public Response getBrandWeb(Long brandId)
{
try
{
Functions.beginTx(tx);
Brand b = entityManager.find(Brand.class, brandId);
if (!b.getSubCollections().isEmpty())
{
b.getSubCollections().size();
}
Response r = null;
if (b != null)
{
r = Response.ok(b).build();
}
else
{
r = Response.serverError().build();
}
Functions.commitTx(tx);
return r;
}
catch (Exception e)
{
return quietRollback(tx, e, Response.class);
}
}
}