How does a client create a new Enterprise Bean instance? Or find an existing one (especially for entity beans that represent database data)? The client needs a factory to create and locate beans without knowing the implementation details.
The Home Interface is the factory interface that defines methods for creating, finding, and removing Enterprise Bean instances. It acts like a constructor that can create beans on demand.
- Bean provider defines the Home Interface (in EJB 2.x style, extends EJBHome)
- Container implements the Home Interface automatically
- Client obtains Home Interface reference via JNDI lookup
- Client calls create() methods on Home to get new bean instances
- For entity beans, client calls finder methods to locate existing beans
- Home Interface handles instantiation, but actual bean instance is wrapped by EJB Object
- Factory pattern: Creates new bean instances on demand
- Finder methods: For entity beans, locates existing persistent objects
- Lifecycle control: Also handles bean removal (remove() method)
- Local/Remote: Can have LocalHome and RemoteHome variants
- Container-implemented: Developer defines interface, container provides implementation
- In EJB 3.x: Largely replaced by annotations like @Stateless, @Stateful
- Builds into: EJB Object — Home creates instances wrapped by EJB Object
- Builds into: Entity Bean — Home finds entity beans by primary key
- Built from: EJB Container — container implements Home, EJB Development Lifecycle — Home defined in step 1
- Related: Finder Methods — specific to entity beans, Local Home Interface — high-performance same-JVM version
- Contrasts with: Remote Interface — Remote defines business methods, Home defines creation/factory methods, Why Bean Doesn't Implement Component Interface
- In EJB 3.x, Home Interface is simplified — annotations replace most of it
- For stateless beans, create() typically returns same pooled instance
- For entity beans, finder methods return bean references identified by primary key
- Home is NOT the bean itself — it’s just the factory