0
votes

my question is rather architectural in connection with EJB 3.0 and higher versions with multi-tier applications.

Background: What do you think the best solution would be to pass data from the database over the session facade to the presentation tier? The entity classes detached would offer for me an attractive solution but in this case they should be specified as argument types in the session facade methods. Consequently, the enity classes should also be given to the presentation tier or any other client as a jar that would like to connect to the session facade. Somehow I don't feel it is the right way because they contain much more that the presentation tier should be aware of but I am not sure as they are detached and light-weight classes and represent also the relationships and contain also the related collections of classes what could spare a lot of work with transfer objects to rewrap them.

Question: Pre-EJB 3.0, in the age of the enity beans, the data should have been rewrapped in Transfer Objects and the Transfer Object Assembler design pattarn could also have been used to build the complex data structures to model n-m or 1-n relationships. However, the entity classes are much more light-weigth and we could spare this rewrapping. What do you propose for providing the data to the presentation tier, how do you do it usually?

Thank you a lot for your response. Kind Regards, Tamas

1
Why not go for a layered architecture : Model(Entities)-Service/Business(EJB3)-Controllers(something like JSF managed beans)-Presentation - Hichamov
Hi Hichamov, thanks for the fast response. Could you please give a little more details? With this solution should also get the data from the entities to the controller/JSF somehow. Or I misunderstand your proposal. Thanks again, Tamas - Tamas
Since I am giving more details, I wrote an answer. - Hichamov
In a project where I worked, we added the project with ejb entities as a build path entry(or i'd say add as dependance so they are available in the class path) so the presentation tier/middle layer would have information about the entities. I dont think there is any risk in doing that unless the presentation layer is being developed by the 3rd party and you'd like to hide the details. I've seen a few others do it it in the same fashion. Hope this helps. - Zeus
Thanks for this comment. If you add it as an answer, I will mark it as solution to the question. - Tamas

1 Answers

1
votes

Ok, so I'll be answering based on my JSF experience. First you create your entities in the model layer, second you create your EJBs in the service layer accessing the entities from the entitymanager (persistence context : https://docs.oracle.com/html/E13981_01/usclient005.htm). In the JSF case, each server page (.xhtml) has acces to managed beans (like follow: http://docs.oracle.com/javaee/6/tutorial/doc/bnaqm.html). The Managed beans has access to the EJBs (look at CalculatorBean here : http://tomee.apache.org/examples-trunk/jsf-managedBean-and-ejb/README.html).

That way you have a very good layered architecture, with loose coupling ans easier maintenance. But remember I am only answering for a JSF environment.

EDIT

If you mean how to access your entity classes data in EJBs (stateful, stateless or singleton), then you will have to manage entities in the EJBs and populate them/find them/perist them/update them using an entitymanager

EJB example:

@Stateless

public class EmployeeDemoSessionEJB implements EmployeeDemoSession

{

         @PersistenceContext 

         EntityManager em; 

         public void createEmployee(String fName,String lName){ 

                      Employee e = new e(); 

                      e.setFirstName(fName); 

                      e.setLastName(lName); 

                      em.persist(e); 
         } 

}

Think of it like a stack : Web page to managed bean to session bean to entity Hope it'll help