views:

181

answers:

2

I am using a JEE6 stack including JPA 2.0, JSF 2.0, EJB 3.1 etc.

The way my architecture is setup is the following:

I have JPA annotated DAOs using hibernate as my JPA provider. I have JSF Managed beans which correspond to my facelet/xhtml pages. I have EJBs that handle all of my database requests.

My XHTML pages have JSF EL which make calls to my Managed beans. My managed beans contain references to my DAO entities which are managed by EJBs. For example, I have a user entity which is mapped to a db table. I have a user EJB which handles all CRUD operations that return Users. I have a page that edits a user. The highlevel workflow would be: navigate to user edit page -> EL calls a method located in the managed bean that loads a user. The method calls userEJB.loadUser(user) from the EJB to get the user from the database. The user is edited and submit -> a function is called in the managed bean which calls a function in the EJB to save the user. etc.

I am running into issues accessing my data within my JSF pages using EJBs. I am having a lot of problems with lazy initialization errors, which I believe is due to how I have set things up.

For example, I have a Client entity that has a List of users which are lazily loaded. In order to get a client I call a method in my EJB which goes to the database, finds a client and returns it. Later on i wish to access this clients list of users, in order to do so i have to go back to the EJB by calling some sort of method in order to load those users (since they are lazily loaded). This means that I have to create a method such as

public List<User> getUserListByClient(Client c)
{
    c = em.merge(c); return c.getUserList();
}

The only purpose of this method is to load the users (and I'm not even positive this approach is good or works). If i was doing session management myself, I would like just leave the session open for the entire request and access the property directly, this would be fine as the session would be open anyway, there seems to be this one extra layer of indirection in EJBs which is making things difficult for me.

I do like EJBs as I like the fact that they are controlled by the container, pooled, offer transaction management for free etc. However, I get the feeling that I am using them incorrectly, or I have set up my JSF app incorrectly.

Any feedback would be greatly appreciated.

thanks,

+1  A: 

If i was doing session management myself, I would like just leave the session open for the entire request and access the property directly, this would be fine as the session would be open anyway

Indeed, that's the open session in view pattern (also called open EntityManager in view). It can be used with EJB as well. Ideally, the transactions should be managed in the business layer/EJB so this can be seen a slight deviation from the pure layer architecture. But it solves the problem of lazy loading in the view and is easy.

Otherwise, you must make sure to eagerly load the information that will be used after the transaction is over. Or you may rely on DTO, but then it starts to be cumbersome.

Here are two more links that cover the topic and discuss pros/cons and alternatives:

ewernli
+1  A: 

Your usage seems good. Just remember that em.merge(c) may save the changes made to Client c into the database. If you just want to get the UserList of Client c without saving the changes made to the Client c, then you can do this:

public List<User> getUserListByClient(Client c)
{

     Client client = em.find(Client.class, c.clientId);
     return client.getUserList();

}

Or better ,just send the Client Id to the getUserListByClient instead of passing a full Client object , just to save a tinsy winsy bit of memory :)

nash