Using POJO as a model in JSF and JPA projects, right?

I am developing a project using JSF 2 and JPA 2 (EclipseLink 2.3). In JSF 2, I found out that we have to separate the model from the controller and the same thing before presentation (thanks to BalusC). But now with the POJO generated from the JPA, I wonder if the bean, model, can now be pojos.

My view is my .xhtml pages developed in JSF 2.0 that will interact with my controllers, and then the controllers call the DAO classes and then work in my database.

Is it correct? I mean the concept of MVC? I want to do everything right, any advice?

Thanks in advance.

+4
source share
3 answers

"MVC" has several JSF perspectives. From a high-level perspective, the Model is represented by EJB / JPA and, ultimately, DAO / DTO (if any). The view is represented by your own JSF code (which contains beans and Facelets managed templates). The controller is represented by FacesServlet .

From a low-level view (a further subdivision of a high-level view), the model is represented by objects or DTOs. The view is represented by your Facelets templates. The controller is represented by your managed bean. This is basically M (MVC) C.

Note that “POJO” is a fairly inherited term from the old J2EE / Hibernate. Currently, with Java EE / JPA, they are called "entities." Yes, those are javaabeans. Als notes that some may prefer to use regular DTO vanilla instead of entities that should separate your JSF code from the service level. Then the JSF should use these DTOs as a model, and the service level should, in turn, display data between these DTOs and real objects. This, in my opinion, is not necessary. EJB3 / JPA2 is a fairly smooth API that already minimizes many code templates for which you would use DAO / DTO, for example, as in the old days of J2EE. With Java EE 6 and above, there is no need to switch from a service level, such as Spring. Everything is already well thought out and standardized.

See also:

+6
source

Not quite right. It is usually best to avoid referencing model objects directly in the view; you can replace them with DTO (data transfer object), which simply serve the purpose of storing the displayed data.

+1
source

Well, in my opinion, this is not true. The XHTML page is a view, but the controller is the JSF servlet (already provided by the infrastructure), and what you call the “controller” is actually a model (business logic). JPA POJOS are part of the model (data model).

From the view, you must call the business logic to get the result (like JPA Pojos) and use them directly (without converting to DTO, necessary in your architecture) in the view.

In your model, you can organically manage business logic (if you think you need a DAO layer that is very common in the industry, you can just put it in).

+1
source

All Articles