java EJB - Home/Remote 和 LocalHome/Local 接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/913267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EJB - Home/Remote and LocalHome/Local interfaces
提问by Peter Perhá?
Revising some past exam papers for an exam mainly focus on component-oriented design and J2EE, I have come across the following question:
复习以前的一些试卷,主要针对面向组件的设计和J2EE,我遇到了以下问题:
A preliminary investigation of scenario 3: “Exchange Request” suggests that two EJBs will provide a suitable solution: a session bean called EnterExchangeRequest to control the processing and an entity bean called ExchangeRequest to represent the persistent properties of the request. Discuss the role of the following interfaces:
对场景 3:“Exchange Request”的初步调查表明,两个 EJB 将提供合适的解决方案:一个名为 EnterExchangeRequest 的会话 bean 用于控制处理,一个名为 ExchangeRequest 的实体 bean 用于表示请求的持久属性。讨论以下接口的作用:
- Home
- Remote
- LocalHome
- Local
- 家
- 偏僻的
- 本地家庭
- 当地的
and how they would provide access to the services of the EJBs described above.
以及它们如何提供对上述 EJB 服务的访问。
I could try to explain how Homeand Remoteinterfaces would fit into the picture. I have also heard the lecturer say one could replace Homeby LocalHome, and Remoteby Local(why?), but why are they asking me to discuss the role of all four at the same time?
我可以尝试解释如何Home和Remote界面适合图片。我也听到讲师说,人能够取代Home的LocalHome,并且Remote通过Local(为什么?),但他们为什么要问我在同一时间来讨论四个方面的作用?
Do I get it right when I say, the EJB container(the application server) would see that an interface is Homeor Remoteand then decide that the bean can 'live' on any machine in the cluster, while in the case the interfaces are LocalHomeand Localthe container will know that the beans can't be distributed across multiple machines and will therefore keep them 'alive' in one machine only?
我得到它的权利,当我说,在EJB容器(应用服务器),会看到一个接口Home或Remote再决定该Bean在集群中的任何机器上能“活”,而在案件的接口LocalHome和Local对容器会知道 bean 不能分布在多台机器上,因此只会让它们“存活”在一台机器上?
I am totally lost in this enterprise Java jungle. I am experiencing a BeanOverflow. Could you please tell me which of my assumptions are wrong, point out my misconceptions and blunders.
我完全迷失在这个企业 Java 丛林中。我正在经历 BeanOverflow。你能告诉我我的哪些假设是错误的,指出我的误解和失误。
Thank you all who are willing to help me with these EJB interfaces.
感谢所有愿意帮助我处理这些 EJB 接口的人。
P.S. Note that I am not asking you to answer the question from the past exam paper. Just curious if you have any thoughts as to what could they be after when asking this.
PS 请注意,我不是要您回答过去试卷中的问题。只是好奇你是否有任何想法,问这个问题时他们会追求什么。
回答by Yishai
Home is responsible for the creation of the Remote (kind of like its constructor) and LocalHome and Local have the same relationship.
Home 负责创建 Remote(有点像它的构造函数)和 LocalHome 和 Local 有相同的关系。
In each case the container is giving you a proxy that references the real EJB class that you write.
在每种情况下,容器都会为您提供一个代理,该代理引用您编写的真实 EJB 类。
If I had to guess, what the question was looking for was the use of remote for the session bean and local for the entity bean.
如果我不得不猜测的话,问题是要寻找的是会话 bean 的远程使用和实体 bean 的本地使用。
Anyway, although these concepts can still exists, things have been much better simplified in EJB3.
不管怎样,虽然这些概念仍然存在,但在 EJB3 中已经简化了很多。
EDIT: In response to the comment, with EJB3, the bean class itself can implement the remote and the home interfaces directly (for the session beans). They are made EJB's with a single annotation. Stateful beans have a couple more annotations to deal with state issues. Entity beans do not have a Home interface, and do not need a local interface, you can interact with the java object directly. There is an EntityManager that retrieves the right entity beans based on a query, and that EntityManager is injected via an annotation.
编辑:响应评论,使用 EJB3,bean 类本身可以直接实现远程和本地接口(对于会话 bean)。它们是带有单个注释的 EJB。有状态 bean 有更多的注释来处理状态问题。实体bean没有Home接口,也不需要本地接口,可以直接与java对象交互。有一个 EntityManager 可以根据查询检索正确的实体 bean,并且 EntityManager 通过注解注入。
That kind of sums it up in a paragraph. There are great tutorials on the web for this stuff, but EJBs in general solve a class of problem that is hard to appreciate unless you deal with the problem. They aren't the only way to solve it, but unless you deal with this type of programming, just reading about it won't really help you relate to it.
这种总结在一个段落中。网络上有关于这些内容的很棒的教程,但是 EJB 通常可以解决一类很难理解的问题,除非您处理这些问题。它们不是解决问题的唯一方法,但除非您处理这种类型的编程,否则仅仅阅读它并不能真正帮助您了解它。
回答by Salandur
As pointed out by Yishay, Home/Remote and LocalHome/Local are tied together and the Home interface functions as a constructor.
正如 Yishay 所指出的,Home/Remote 和 LocalHome/Local 是绑定在一起的,Home 接口作为一个构造函数。
Local beans are tied to the JVM they live in, you can not access them from the outside. Remote beans can be accessed from other JVMs.
本地 bean 与它们所在的 JVM 相关联,您无法从外部访问它们。可以从其他 JVM 访问远程 bean。
I use a similar approach: I always deploy ears. Beans for the ear I make local beans, Beans meant for use by other ears I make remote. But it is possible to use the local beans in other ears, as long as the are deployed in the same JVM
我使用类似的方法:我总是部署耳朵。耳朵的豆子我做本地豆子,豆子是我做远程的其他耳朵使用的豆子。但是可以使用其他耳朵的本地bean,只要部署在同一个JVM中即可

