Types of Lazy Loaders
In Hibernate, there are two main components which can be lazy loaded. They are namely Entities and Collections. An Entity represents a relational data in database, whereas a collection represents collection of children for an entity.
In this article, we'll go through the solutions for lazy loading Entities and Collections separately.
Lazy Loading Entities
Hibernate's lazy loading framework contains two main components namely LazyInitializer and Tuplizer. These components are necessary for creating the reference and initializing the data for this reference. In Hibernate terminology, these reference objects are called Proxy Objects.
LazyInitializer and Tuplizer
LazyInitializer is a Hibernate component that helps to generate proxy objects for Entities and can also be used to fetch the underlying entities of these proxy objects. Hibernate provides a couple of abstract implementations for LazyInitializer such as AbstractLazyInitializer and BasicLazyInitializer, to lazy load or fetch data from inside the Hibernate Session. We'll extend the BasicLazyInitializer and create our own initializer component to fetch data from remote client. Before extending it, let's look at some important methods for lazy loading, in the abstract implementation of LazyInitializer component.
* getImplementation() - this method fetches the entity and returns a persistent object (data object in our case) for the fetched entity.
* getSerializableObject() - this method returns a serializable proxy replacement object for your entity. (We'll discuss more on proxy replacement object in coming sections.)
Tuplizers in Hibernate helps to define contract for certain Hibernate based components. The contract tells how to create such components and play around with the component's property values. These components can either be an Entity or a Mapped component (eg: Composite Identifiers). Hibernate have implementations for these components and are called as EntityTuplizer and ComponentTuplizer respectively. These tuplizers demands you to create Proxy Factory for generating proxy objects of Entities.
As we are using Entity in our solution approach we'll use Entity based tuplizers. Now we need to define own EntityTuplizer and also need to provide a representation called as Entity-Mode for it. Entity modes are actually a representation of the domain model in memory, it can be a POJO, DOM4J or Map based representation. As our data objects are going to POJOs, we'll use POJO based representation.
Hibernate already supports such representation through a class called POJOEntityTuplizer. We'll extend this tuplizer and build a proxy factory which will be used to create proxy object representing our Entity. This proxy object will be of type HibernateProxy as Hibernate would expect.
HibernateProxy is a serializable marker interface that expects you to return a LazyInitializer (as talked above) and a serializable Proxy Replacement object during serialization. This Proxy Replacement object is the one which will be shared with remote client as a lazy entity reference.
Overall our aim is to create a Custom LazyInitializer, a Custom Tuplizer, a Custom Proxy Factory, a Custom Hibernate Proxy object and finally a Custom Proxy Replacement object.
|