Servlet 从远程 EJB3 会话 Bean 接收空对象
我确信这是一个初学者错误...
所以我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
...天哪,这有点尴尬...
事实证明,我一直忽略了一个关于使用
Vector
作为包含@ 的字段类型的漂亮小警告xxToMany
与 FetchType.LAZY 的关系:两种可能的解决方案可以修复我的行为:
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 withFetchType.LAZY
:Two possible solutions can fix my behaviour:
FetchType.EAGER
(then I could stay withVector
)List
(as the spec says...)