Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using POJO as Model in JSF and JPA project, is that right?

i'm developing a project using JSF 2 and JPA 2 (EclipseLink 2.3). In JSF 2 I learned that we must separate the Model from Controller and the same thing to the View (thanks BalusC). But now with the POJO's generated from JPA, I wonder if the bean, the model, it should be the pojos now.

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

Is this right ? I mean in the concept of MVC ? I want to do everything right, any tip ?

Thanks in advance.

like image 285
Valter Silva Avatar asked Feb 23 '23 02:02

Valter Silva


2 Answers

"MVC" has in JSF multiple points of view. From the high level view, the Model is represented by EJB/JPA and eventually DAO/DTO (if any). The View is represented by your own JSF code (which consits of managed beans and Facelets templates). The Controller is represented by the FacesServlet.

From low level view (a further subdivision of the high level View), the Model is represented by entities or DTOs. The View is represented by your Facelets templates. The Controller is represented by your managed bean. It's basically a M(MVC)C.

Note that "POJO" is a rather legacy term from the old J2EE/Hibernate times. Nowadays, with Java EE/JPA, they're called "Entities". Yes, it are those @Entity Javabeans. Als note that some may opt to use plain vanilla DTOs instead of entities which should decouple your JSF code from the service layer. JSF should then use those DTOs as model and the service layer should in turn map between those DTOs and the real entities. This is in my opinion not necessary. EJB3/JPA2 is a pretty slick API which already minimizes a lot of boilerplate code for which you would have used DAOs/DTOs like as in old J2EE ages. With Java EE 6 and higher, there's not really a need to be able to switch of service layer to for example Spring. Everything is already well thought out and standardized.

See also:

  • What components are MVC in JSF MVC framework?
  • Difference between DTO, VO, POJO, JavaBeans?
  • I found JPA, or alike, don't encourage DAO pattern
like image 169
BalusC Avatar answered Feb 25 '23 16:02

BalusC


Not completely right. It's usally better to avoid referencing model objects directly in the view; you can substitute them with DTO(data transfer object) that just serve the purpose of containing the data to be displayed

like image 37
Simone Avatar answered Feb 25 '23 16:02

Simone