springspring-data-jpaentity-bean

Is it a good practice to use the entity beans for front end


I'm using Thymeleaf for frontend and Spring + Hibernate for the backend.

Instead of creating DTOs is it advisable to use the same entity beans for frontend. For attributes that are exclusive for frontend, we could use @Transient anyways, right?

I noticed that in PetClinic Spring Demo application they had reused the models.


Solution

  • Entities are Part of Domain Driven Design (DDD) and Important aspect of design of it is to have ID. These are meant to represent your entities which is long lived objects in system i.e. in DB, Storages etc.

    Value Objects / Data Transfer Objects are actually meant for temporary calculations and short lived transport such as to use in Views.

    I guess you will be using MVC in your project. It this aspect using Entity which is after service layer will tightly coupled entities with frontend. Which in turn against flexibility of MVC. Better refrain from it.