关于ejb 3.0:Servlet从远程EJB3会话Bean接收空对象

Servlet receives empty object from Remote EJB3 Session Bean

我确定这是一个初学者错误...

因此,我有一个Java EE 6应用程序,该应用程序具有实体,外观(实现持久层)和具有远程接口(提供通过外观访问实体的状态)的无状态会话Bean(EJB3)。

这很好。通过SLSB,我可以检索和操纵实体。

现在,我正在尝试从Web应用程序执行此操作(部署在同一Glassfish中,来自作为单独jar导入的Java EE应用程序的实体接口定义)。我有一个Servlet,它接收注入的SLSB实例。我得到它来检索实体,然后发生以下情况(我可以在日志中看到它):

  • 实例化远程SLSB,其方法称为
  • SLSB实例化外观,调用" get"方法
  • Facade从DB中检索实体的实例,并将其返回
  • SLSB将实体的实例返回给调用者

    • (直到这里一切都很好)
  • 调用servlet接收..实体的空实例!

出了什么问题?这应该可行,对吧?

MyServlet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
    }
  }
  ...
}

请让我知道是否要查看其他代码,我将更新该帖子。


...天哪,这有点令人尴尬...

事实证明,我一直在忽略关于将Vector用作与FetchType.LAZY保持@xxToMany关系的字段类型的小警告:

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.

两种可能的解决方案可以解决我的问题:

  • 使用FetchType.EAGER(然后我可以留在Vector上)
  • 使用List(如规范所示...)