mercredi 20 février 2019

Is using DTO's obsolete with JPA and JSF

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.

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

Aucun commentaire:

Enregistrer un commentaire