0

I am bit confused how to setup a project correctly regarding data flow between Backend (JPA) and Frontend (JSF)

In my old schooltime I learned using DAO's (Repository, Service) and DTO's, but actually I dont see any need of using DTO's anymore. Thats why I am asking here for consulting me howto setup the general framework using JPA and JSF.

I have the following situation:

An Entity Car:

import javax.persistence.*;
import javax.validation.constraints.Size;
   @Entity
    @Table(name = "cars")
    public class Car {

        @Id
        @Size(min = 1)
        private String identifier;

a Repository with basic functionality accessing the DB:

@Stateless
public class CarRepository implements Serializable {

    @PersistenceContext
    private EntityManager entityManager;

    public List<Car> getAllCars(){
        Query q = entityManager.createQuery("Select c from Car c");
        return q.getResultList();
    }

a managed bean with sessionscope:

@ManagedBean(name = "carlistbean")
@SessionScoped
public class CarListBean implements Serializable {

private List<Car> cars= new ArrayList();

    @PostConstruct
    public void init(){
        cars = carRepository.getAllCars();
    }

In my facelet I am referencing the cars:

<h:dataTable value = "#{carlistbean.cars}" var = "car"

As you can see I am skipping the DTO part (for my understanding), but in the other way I am "mixing" Beanvalidation and JPA definitions in the Entity.

My Car Entity has a bidirectional relation to another entity Manufacturer, which I can access using the getter method. So no need to create a composite DTO for that ?

Is this bad, acceptable, good design ? How would you structure the data flow in 2019 ?

mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • How will you show the data which is come from two tables/two entity into same datatable? It is depend on your usage. – Zaw Than oo Feb 20 '19 at 09:34
  • Good question. Thanks for helping. I have a bidirectional Relation defined @oneToMany and ManyToOne. So I could access the other entity directly. – mcfly soft Feb 20 '19 at 09:37
  • If you try to ORM (mapping) for UI / client data, your ORM might be complex. For my experience, Data model is different with UI display / client data. – Zaw Than oo Feb 20 '19 at 09:48
  • In many cases the plain JPA ORM mapping is usable in UI's too. So use it when you can, add/create additional classes (or just transient fields when needed). Works for me in most cases. – Kukeltje Feb 20 '19 at 10:28
  • Possible duplicate of [How to use DTO in JSF + Spring + Hibernate](https://stackoverflow.com/questions/5722036/how-to-use-dto-in-jsf-spring-hibernate) – Peter Šály Feb 20 '19 at 13:07
  • Please read [why JSF calls getters multiple times](https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times) – Jasper de Vries Feb 20 '19 at 14:08

1 Answers1

1

Following the JSF and Java EE experts advises like Bauke Scholtz How to use DTO in JSF + Spring + Hibernate and Adam Bien How evil are Data Transfer Objects it is better to not use DTO objects.

Peter Šály
  • 2,848
  • 2
  • 12
  • 26