Servlet 从远程 EJB3 会话 Bean 接收空对象

发布于 2024-08-29 12:37:19 字数 1167 浏览 7 评论 0原文

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

所以我有一个 Java EE 6 应用程序,其中包含实体、外观(实现持久层)和带有远程接口(通过外观提供对实体的访问)的无状态会话 Bean (EJB3)。

这工作正常。通过 SLSB,我可以检索和操作实体。

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

  • 远程 SLSB 被实例化,其名为
  • SLSB 的方法实例化外观,调用“get”方法
  • 外观从数据库检索实体实例,返回它
  • SLSB 将实体的实例返回给调用者
    • (在此之前一切都很好)
  • 调用servlet接收..实体的空实例

出了什么问题?这应该有效,对吧?

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();
    }
  }
  ...
}

如果您想查看其他代码,请告诉我,我会更新帖子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-09-05 12:37:19

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

事实证明,我一直忽略了一个关于使用 Vector 作为包含 @ 的字段类型的漂亮小警告xxToMany 与 FetchType.LAZY 的关系:

元素[field someField]内
实体类 [class Campaign] 使用
集合类型[类
java.util.Vector]当JPA
规范仅支持
java.util.Collection,java.util.Set,
java.util.List 或 java.util.Map。
这种类型受 eager 支持
加载中;使用延迟加载与此
集合类型需要额外
配置和 IndirectContainer
扩展 [类
java.util.Vector] 或设置
映射以使用基本间接和
类型为 ValueholderInterface。

两种可能的解决方案可以修复我的行为:

  • 使用 FetchType.EAGER (然后我可以继续使用 Vector
  • 使用 List (正如规范所说...... .)

... 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...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文