JSF and SessionBean
I am having an application that uses JSF and hibernate. I would like an advice for its architecture. In JSF we have the managed beans.Lets say I
want to call a method createUser(). I would call it from the managed bean and then?I should use a delegate-->SessionBean-->DAO?
Or is it ok if I call this method from the managed bean and then go directly to the DAO? What is the benefit of using a SesssionBean?
Thanking you in advance.
[462 byte] By [
juanitaJa] at [2007-11-26 17:39:05]

# 1
Hi,
It sounds like your application is not using EJBs. Basically, when you build an app you can choose whether to have a web-only architecture or whether to use the EJB container.
1) EJB style architecture for your app
If you choose the EJB style of architecture then you app might be like
JSFPages-->ManageBeans-->SessionBean>>Java Persistence(or in your case hibernate)
For simple cases, the Session Bean can seem like an extra class but for more complex cases it can be a facade and be a benefit.
2) Web-only style architecture for your app (not using any EJBs)
For web-only, you could have something like this for simple cases
JSFPages-->ManageBeans>>Java Persistence(or in your case hibernate)
For this Web-only style, as an application gets a bit bigger and more complex you might want to use the facade pattern to clean it up a bit. In this case you might add a web component as a facade (instead of a Session Bean) . For a web-only application, you could have a facade class and it does not have to be an EJB.
JSFPages-->ManageBeans>> Facade (POJO or web component) --> Java Persistence(or in your case hibernate)
So bothe styles, 1) and 2) may use a facade.
The facade pattern in Java EE 5 is discussed in https://blueprints.dev.java.net/bpcatalog/ee5/persistence/facade.html
There are pros and cons to whether you choose a web-only architecture or an EJB style arcitecture. Most of it has to do with developer skills and preferences(what technologies you are comfortable with) .
Here are a couple pros/cons between choosing 1) or 2)
--Cons of using EJBs: you have to know another technology and learn EJBs. Learning another adds to complexity of getting started. The app must run on an EJB container so cant run on just Tomcat for example.
--Pros of using EJBs (in EJB 3 part of Java EE 5)have some services that make developing an app a bit easier, like container managed transactions, container-managed entity managers, dependency injection, some Java EE annotations, and a few other services. Easier integration with Java Persistence APIs.
--Cons of using web-only are that you have to write code to use JTA APis to manage transactions, and code to managed entity managers (part of Java Persistence)
Also, Java Persistence APIs http://java.sun.com/javaee/overview/faq/persistence.jsp are a newer technology in Java EE 5 that is sort of a standard version of hibernate capabilities. You could use hibernate with new Java Persistence but in a Java EE 5 application you might not need hibernate, especially if you are using EJBs and Session Beans.
hth,
Sean
> I am having an application that uses JSF and
> hibernate. I would like an advice for its
> architecture. In JSF we have the managed beans.Lets
> say I
> want to call a method createUser(). I would call it
> from the managed bean and then?I should use a
> delegate-->SessionBean-->DAO?
> Or is it ok if I call this method from the managed
> bean and then go directly to the DAO? What is the
> benefit of using a SesssionBean?
>
> Thanking you in advance.
# 2
Thank you very much for your answer.
I have used in the past Session Beans but the truth is that I haven't completely understood their benefits. Of course it creates a more neat image for your application. You mentioned that EJBs offer container managed transactions and container-managed entity managers. If I have a Jboss server can I have these benefits without using Session Beans? I thought that the container managed transactions are a benefit of the application server and not of the Session bean. Moreover I read that if my Session bean resides in the same machine with my Entity Beans(or my Pojos) then we have local calls but if I didn't have Session Beans then I would have remote calls from my client.Is this correct? Can you also please provide me some othet advantages of using stateless Session Beans?
Thank you.
# 3
Hi,
Yes, the benefits of container managed transactions and container-managed entity managers are part of the Java EE application server. But you can not use them on every type of class. In Java EE app servers, the standard way to use these services is on EJB components. JTA provides the transaction management under the hood, and using deployment descriptors or annotations on an EJB component such as a Session Bean is how you use the container managed transactions(which use JTA under the hood).
There may be some non-standard frameworks that could extend that capability and offer cantainer managed transactions and container managed entity managers on web components or other types of Java classes. But they are only available on EJB components in standard Java EE. Hopefully, IMHO, that will be available in Java EE 6 in the future as a stnadard service, but it is not in Java EE 5.
So for web-only applications with no EJBs, there is no container managed transactions, and instead you have to use JTA calls in your code (application-managed transactions)
public class MyServlet extends HttpServlet {
@Resource UserTransaction utx; //or JNDI lookups
and then in some method you begin,commit, rollback or close a transaction like this
public void doGet(....)
try {
utx.begin();
......//do something
utx.commit();
} catch(Exception exe){
System.err.println("Error persisting item: "+exe);
try {
utx.rollback();
} catch (Exception e) {
...
} finally {
em.close();
}
}
So for web components you have to write the JTA code to manage the transaction.Whereas with container managed transactions you do not have to write the code for JTA, it is done under the hood by the container. But this can only be done for EJB components such as Session Beans
hth,
Sean
# 4
Hi,
>Moreover I read that if my Session bean resides in the same machine with my >Entity Beans(or my Pojos) then we have local calls but if I didn't have Session >Beans then I would have remote calls from my client.Is this correct?
That statement confuses me a bit. In general, when a web component such as JSF managed bean or a Servlet needs to access an EJB component such as a Session Bean it can choose whether that access is Local or Remote. You specify whether it is local or remote on the Session Bean by using either the @Local or the @Remote annotations in Java EE 5(or you can also specify it in deployment descriptors). If you do not spoecify anything and you dont put any annotation, then by default the Session Bean is Local.
In general, Local access should be used when the App server (web container & EJB container and other services of Java EE) are all deployed on the same machine(in the same VM). This way the JSF components and the EJB components are running on the same server so can use local access.
JSF Managed Beans --local--> Session Bean --local--> POJO entity
In general, remote access(RMI-IIOP) to a Session Bean is used when the web server is running on a different machine from the EJB server. This is not a very common case.
JSF Managed Beans --Remote(RMI-IIOP)--> Session Bean --local--> POJO entity
>Can you also please provide me some othet advantages of
>using stateless SessionBeans?
I am not sure if there are really advantages or disadvantages. Its really a matter of what you prefer. If you like EJBs like Session Beans then you can use them.
Let me know if that answers your questions.
hth,
Sean
# 5
First thank you for your answers. So from what I have understood the benefits that Jboss for example offers for managing the transactions can be used when Session Beans are used otherwise the developer has to take care of it by himself.(eg beginTransaction())
What I did not understand is when you are saying that we have Remote calls when the web container and the application server are on different machines?How can this be happening? The web container and the application server are not the same such as JBoss?
# 6
Hi,
All Java EE applications servers(Oracle, Sun, JBoss, etc) have the common capabilities and features defined in the Java EE specifications. So the things I am saying would apply to all Java EE 5 applications servers.
> offers for managing the transactions can be used when
> Session Beans are used otherwise the developer has to
> take care of it by himself.(eg beginTransaction())
Yes. That is true for all Java EE application servers. Container-managed transactions are a standard feature of EJBs(Session Beans etc), and container-managed transactions are not a standard feature of web components(Servlets, JSF, etc)
> What I did not understand is when you are saying that
> we have Remote calls when the web container and the
> application server are on different machines?How can
> this be happening? The web container and the
> application server are not the same such as JBoss?
A Java EE application server(Oracle, Sun, JBoss, etc) is made up of several services and containers. There is a web server, an EJB server, a transaction manager, a JMS server, JNDI server, etc that are all part of the application server. In Java EE application servers, you can choose to deploy all the parts of an application server on one machine, or you can deploy them distributed on different machines. For example, someone could deploy the web container on one machine and the EJB container on another machine. Java EE allows you to have either a local or a distributed(remote calls) architecture.
The more common case to consider is when the EJB container and web container of the application server are running on one machine. Local access between the web container and EJB container is a lot faster and you dont have to deal with issues of distributed computing.
You can check out this page from our previous book that discusses choosing a local or distributed architecture(Note the boojk talks about a much older version of petstore, and not the petstore 2.0 application architecture.) But this might help to understand the basic issue of choosing a distributed(remote calls) or local architecture.
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/app-arch/app-arch4.html
hth,
Sean
# 7
Ok I thing I understood. Thank you very much for your replies.