I’m sure this is a beginner error…
So I have a Java EE 6 application with entities, facades (implementing the persistence layer) and Stateless Session Beans (EJB3) with Remote interfaces (providing access to the entities via facades).
This is working fine. Via the SLSB I can retrieve and manipulate entities.
Now, I’m trying to do this from a Web Application (deployed on the same Glassfish, entity+interface definitions from Java EE app imported as separate jar). I have a Servlet, that receives an instance of the SLSB injected. I get it to retrieve an entity, and the following happens (I can see it in the logs):
- the remote SLSB gets instantiated, its method called
- SLSB instantiates the facade, calls the ‘get’ method
- facade retrieves instance of entity from DB, returns it
- SLSB returns the instance of the entity to the caller
- (all is good until here)
- calling servlet receives .. an empty instance of the entity !!
What is going wrong? This should work, right?
MyServlet:
public class MyServlet extends HttpServlet {
@EJB
private CampaignControllerRemote campaignController; // remote SLSB
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
try {
Campaign c = campaignController.getCampaign(5L); // id of an existing campaign
out.println("Got "+ c.getSomeString()); // is null !!
} finally {
out.close();
}
}
...
}
Pls let me know if you want to see other code, and I’ll update the post.
… oh boy, this is sort of embarrassing …
It turns out, I have been ignoring a nice little warning regarding the use of
Vectoras type of a field that holds a@xxToManyrelationship withFetchType.LAZY:Two possible solutions can fix my behaviour:
FetchType.EAGER(then I could stay withVector)List(as the spec says…)