Common Expenses: Lazy Loading

When developing the POJO classes for mapping the database I set the fetch type to LAZY. At first i didn’t pu much interes on it but then when I was actually fetching the data from DB I had to lern it better.

The target was to display all project’s data on a page, my first idea was to simply retrieve the project from db

(Project)session.get(Project.class, 1)

and then display it:

<c:forEach var="expense" items="${project.expenses}">

But this didn’t worked. When an entity is instanciated all its collelctions are defined but the content is not loaded, infact lazy loading means Hibernate would fill the collenctions when them are requested.

If you try to retrieve all project’s expenses into the EJB there are no problems as an Hibernate session is opened. When we are into the view context (JSP in this case) there are no Hibernate sessions opened that’s why the expense collection could not be retrieved.

If the fetch mode is set to EAGER Hibernate load all entities collection whe the entity itself is istanciated. The view above would display all the data correctly but if we add a new expense it won’t be added to the collection as the main entity has already benn loaded.

The best solution would be create a Filter which open and close an Hibernate session at view context to lazy load the collections whenever are needed but seems like this way-around exists only in Spring Framework.

The fixeed this problem with in a ‘dirty’ way building a emporary container which collect the projects data every time the page is requested. This is not as performant as lazy loading because it fetches all the data at each request. But it finally make the view display all data.

Common Expenses: Stateful EJB, fail

Here we come with bad notes.

I was not able to implement some logic with Stateful Session Bean in my application. First it was difficult to find a place for such a thing inside my application then I wasn’t able to find good instruction on how to couple Stateful Bean with Servlet.

Common Expenses: Design Pattern

This post should have been one of the first actually.

Here is described the design pattern used on Common Expenses, I’ve used MVC as it is very straight forward: separate data management (model), application login (controller) and presentation (view).

As I post earlier the persisten layer is made with Hibernate, then the Businnes Login is implementend via Stateless EJB.

The application controller is a set of Servlet which get requests, work out them and send the result to the presentation layer.

The presentation layer is implemented with JSP.

Here is a schema of the whole application:


Common Expenses: EJB

To communicate with POJO entities I’m going to use EJB version 3.1.

As done before I will use annotation instead of a XML configuration file.

I will come back later with a full post on how to couple EJB with Hibernate.

Essentialy we need an Interface for make the bean accessible from other tools (in this case I’ll use Servelt to access EJB):

public interface RegisterBeanRemote

Remote means that this is he ‘server‘ class, in this instance our client will be the Servlets but if it would have been another java application we would have to distinguish between Remote and Local.

When the interface is ready we need a class to implement it:

public class RegisterBean implements RegisterBeanRemote

Stateless means that the bean doesn’t keep a ‘memory‘.

There’s no need to use JNDI explixity because annotation will do it for us.