views:

215

answers:

1

I'm sure this is a beginner error...

So I have a JEE6 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 JEE 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.

A: 

... oh boy, this is sort of embarrassing ...

It turns out, I have been ignoring a nice little warning regarding the use of Vector as type of a field that holds a @xxToMany relationship with FetchType.LAZY:

Element [field someField] within entity class [class Campaign] uses a collection type [class java.util.Vector] when the JPA specification only supports java.util.Collection, java.util.Set, java.util.List, or java.util.Map. This type is supported with eager loading; using lazy loading with this collection type requires additional configuration and an IndirectContainer implementation that extends [class java.util.Vector] or setting the mapping to use basic indirection and the type to be ValueholderInterface.

Two possible solutions can fix my behaviour:

  • use FetchType.EAGER (then I could stay with Vector)
  • use List (as the spec says...)
Hank