Client View of Bean  «Prev  Next»

Lesson 3The home object and the home interface
ObjectiveDescribe the Home Object and the Home Interface.

Describe the Home Object and Home Interface

The concept and usage of the Home Object in Enterprise Java (formerly J2EE, now Java EE) have significantly evolved between J2EE and Java EE 8.
J2EE (Java 2 Platform, Enterprise Edition): In J2EE, Enterprise JavaBeans (EJBs) were heavily dependent on the concept of a Home Object (or Home Interface). The Home Object was responsible for the lifecycle management of EJBs, such as creating, finding, and removing EJB instances. Every EJB in J2EE had an associated Home Interface, which clients used to create or locate instances of the EJB.
For example:
  • Session Beans: The Home Interface provided methods to create EJB instances.
  • Entity Beans: The Home Interface included methods to create, find, or remove entity instances.

Java EE 8: By the time Java EE 8 was released, the EJB specification had undergone significant changes, and the concept of Home Objects had been largely deprecated and replaced by more modern practices.
  1. Simplification: Java EE 5 introduced simplified EJB development, removing the need for Home Interfaces altogether. Developers could now use annotations and dependency injection to manage EJB lifecycles, which made the Home Object redundant.
  2. Dependency Injection: Java EE 8 fully embraced dependency injection, which allowed EJBs and other components to be injected directly into managed classes like servlets, other EJBs, and CDI (Contexts and Dependency Injection) beans without the need for a Home Object.
  3. Annotations: The use of annotations such as `@Stateless`, `@Stateful`, and `@Singleton` allowed developers to define EJBs directly within a class, without the need for separate interfaces or Home Objects. This made the EJB model much more lightweight and easier to use.

Summary The Home Object, which was central to EJBs in J2EE, became obsolete as of Java EE 5 and onwards, including in Java EE 8. The shift towards annotations and dependency injection made EJB development much more straightforward, removing the need for the Home Interface and making enterprise development easier and more accessible. These changes reflect a broader trend in Java EE toward reducing complexity and improving developer productivity.


Skeleton of the home interface

Here is part of the home interface for the Hello bean created by the Hello bean developer:
import javax.ejb.EJBHome;
public interface HelloHome extends EJBHome {
 .
}

Note that it is an interface, not a class.
The real home object class will be created as part of the deployment process. The container will instantiate the home object instance when the bean is installed into the container. The home interface always extends EJBHome for structural reasons. EJBHome includes methods to access the bean's metadata [1] .
  • Remote objects: A remote object[2] is an object that has
    1. a stub and
    2. a skeleton
    and is accessible over the network.
    The location of the object needs to be known before its methods can be called.


Both the skeleton and the stub are uniquely associated with that specific remote object. When a client wishes to communicate with the remote object, it will use its unique stub, often known as a remote reference[3]. In Java terms, the remote reference is the reference to the stub instance in the JVM[4]. This is illustrated in the following diagram.
Client Remote reference communicates with the remote object over the Network.
Client Remote reference communicates with the remote object over the Network.
  1. To access a remote object, the program needs its stub.
  2. To access EJBs, the program needs stubs for the home object and EJBObject.

Data type of the stub

The data type of the stub is unknown to the client. From the proxy pattern, the stub and the entity for which it is a proxy have the same interface.
Thus, if we know the interface of the remote object, we know the interface implemented by the stub. Therefore, considering the HelloHome interface code above, the Java type of the stub to the home object is HelloHome. We can write code as follows:
HelloHome home = (HelloHome) nameService.lookup("hello");

  • Advice on EJBHome Examine now the methods of the EJBHome class in the javadoc J2EE API documentation to help you understand the methods inherited by the Home interface. In the next lesson, you will apply the reasoning above to the actual lookup of the home object from the Name Service.
[1]Metadata: The information about an object: its methods, parameters, return types, etc.
[2]Remote object: An object that provides a service through its methods and is located on another host on the network.
[3]Remote reference: The reference to an object, usually the stub, that knows where, and how to communicate with, a remote object.
[4]JVM: The Java Virtual Machine, a software implementation of a portable environment, that executes Java programs.

SEMrush Software