Am I using EJBs properly?

Posted by kgrad on Stack Overflow See other posts from Stack Overflow or by kgrad
Published on 2010-04-22T18:22:49Z Indexed on 2010/04/22 19:03 UTC
Read the original article Hit count: 409

Filed under:
|
|
|

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,

© Stack Overflow or respective owner

Related posts about java

Related posts about jee6