Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Entity and DTO

Tags:

java

entity

dto

What is the difference between a DTO and an Entity? In details these are my questions:

  1. What fields should the DTOs have? For example my entity classes are:

    @Entity public class MyFirstEntity implements Serializable {      @Id @GeneratedValue     private Long id;      private String stringData;      @OneToOne     private MySecondEntity mySecondEntity;      @OneToMany     private List<MySecondEntity> mySecondEntitesList;  }  @Entity public class MySecondEntity implements Serializable {      @Id @GeneratedValue     private Long id;      private Integer integerData;      @ManyToOne     private MyFirstEntity myFirstEntity;  } 

There is a one-sided connection (One-to-one) and a two-sided connection (Many-to-one), a simple String and Integer data and of course the ids. What to put from them in the MyFirstDTO and MySecondDTO classes?

  1. If there is an inheritance between the entities, then how should I represent it in the DTOs? For example:

    @Entity public class MyFirstEntity extends MySecondEntity {     .... }  @Entity public class MyFirstDTO extends MySecondDTO {     .... } 
  2. How should I use them? For example, I find out this: I'm working on a web project. The user of the webpage wants to register. He/She fills the forms, and sends it to the server. On the server side I create first a DTO, because its fields have the validations. From the DTO I create an Entity and persist it to the database. When there is a request for an entity, I convert the requested entity to DTO, and give it to the user on the client side. Is it a good imagination, or not?

like image 201
Display Name Avatar asked Sep 08 '16 17:09

Display Name


People also ask

Can we use entity as DTO?

While EJB3 allowed for the possibility of Entities being used as DTOs, the truth is it is still better to have separate DTOs. Pretty soon you will find that your Entities will require annotations related to specific JPA Providers (like Hibernate) which will tie your Entities to the persistence layer.

What is difference between DTO and model?

Data Transfer Objects (DTOs) and View Models (VMs) are not the same concept! The main difference is that while VMs can encapsulate behaviour, DTOs do not. The purpose of a DTO is the transfer of data from one part of an application to another.

What is a DTO used for?

In the field of programming a data transfer object (DTO) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation.

What is difference between entity and model?

Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables. Model: A model typically represents a real world object that is related to the problem or domain space.


1 Answers

Short answer:

  • Entities may be part of a business domain. Thus, they can implement behavior and be applied to different use cases within the domain.

  • DTOs are used only to transfer data from one process or context to another. As such, they are without behavior - except for very basic and usually standardised storage and retrieval functions.

Long answer:

While the term "Data Transfer Object" (DTO) is defined quite unambiguously, the term "Entity" is interpreted differently in various contexts.

The most relevant interpretations of the term "Entity", in my opinion, are the following three:

  1. In the context of enterprise java and jpa:
    "An object that represents persistent data maintained in a database."

  2. In the context of "domain-driven-design" (by Eric Evans):
    "An object defined primarily by its identity, rather than its attributes."

  3. In the context of "clean architecture" (by Robert C. Martin):
    "An object that encapsulates enterprise-wide critical business rules."

The Jee- and Jpa-community sees entities primarily as objects mapped to a database table. This point of view is very close to the definition of a DTO - and that's where much of the confusion probably stems from.

In the context of domain-driven-design, as well as Robert Martins point of view, however, Entities are part of a business domain and thus can and should implement behavior.

like image 93
Andreas Vogl Avatar answered Sep 20 '22 06:09

Andreas Vogl