Java 实体和 DTO 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39397147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Entity and DTO
提问by Display Name
What is the difference between a DTO and an Entity? In details these are my questions:
DTO 和实体有什么区别?详细来说,这些是我的问题:
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; }
DTO 应该有哪些领域?例如我的实体类是:
@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?
有一个单边连接(One-to-one)和一个双边连接(Many-to-one),一个简单的 String 和 Integer 数据,当然还有 ids。什么,从他们把在MyFirstDTO
和MySecondDTO
班?
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 { .... }
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?
如果实体之间存在继承,那么我应该如何在 DTO 中表示它?例如:
@Entity public class MyFirstEntity extends MySecondEntity { .... } @Entity public class MyFirstDTO extends MySecondDTO { .... }
我应该如何使用它们?例如,我发现了这一点:我在做一个网络项目。网页的用户想要注册。他/她填写表格,并将其发送到服务器。在服务器端,我首先创建了一个 DTO,因为它的字段有验证。我从 DTO 创建了一个实体并将其保存到数据库中。当有实体的请求时,我将请求的实体转换为 DTO,并在客户端将其交给用户。这是一个很好的想象力,还是不是?
回答by Shatayu Darbhe
Difference between DTO & Entity:
DTO与实体的区别:
Entity is class mapped to table. Dto is class mapped to "view" layer mostly. What needed to store is entity & which needed to 'show' on web page is DTO.
实体是映射到表的类。Dto 是主要映射到“视图”层的类。需要存储的是实体,并且需要在网页上“显示”的是 DTO。
Example: If I want to store employee model as follows : Take employee as an example, I need to store gender either male/female/other. But on JSP I need to show all three values as form 'options' so user can select one.
示例:如果我想存储员工模型如下:以员工为例,我需要存储男性/女性/其他性别。但是在 JSP 上,我需要将所有三个值都显示为表单“选项”,以便用户可以选择一个。
@Entity
public class Employee{
//annotate with @Id and others
private Long id;
private String name;
private Gender gender; //this is enum viz Male,female
}
//Now extend Dto with employee
public EmployeeDto extends Employee{
Gender[] genders=Gender.values(); //put all gender types in array.
}
while rendering jsp we can give
在渲染jsp时我们可以给出
<select name="gender"> //pointed towards entity gender field.
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
then in spring or some other framework whichever selected will be opted as gender in entity.This is made possible because Dto had all three values of gender in it. Similarly, as per situation things follows. As mostly we need most of entity fields on jsp we extend dto by entity.
然后在 spring 或其他框架中,无论选择哪个都将被选为实体中的性别。这是因为 Dto 具有所有三个性别值。同样,根据情况,事情如下。 由于大多数情况下我们需要 jsp 上的大部分实体字段,因此我们按实体扩展 dto。
回答by Andreas Vogl
Short answer:
简短的回答:
Entities may bepart 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.
实体可以是业务域的一部分。因此,它们可以实现行为并应用于域内的不同用例。
DTO 仅用于将数据从一个进程或上下文传输到另一个进程或上下文。因此,它们没有行为——除了非常基本且通常标准化的存储和检索功能。
Long answer:
长答案:
While the term "Data Transfer Object" (DTO) is defined quite unambiguously, the term "Entity" is interpreted differently in various contexts.
虽然术语“数据传输对象”(DTO) 的定义非常明确,但术语“实体”在各种上下文中的解释不同。
The most relevant interpretations of the term "Entity", in my opinion, are the following three:
在我看来,对“实体”一词最相关的解释是以下三种:
In the context of enterprise java and jpa:
"An object that represents persistent data maintained in a database."In the context of "domain-driven-design" (by Eric Evans):
"An object defined primarily by its identity, rather than its attributes."In the context of "clean architecture" (by Robert C. Martin):
"An object that encapsulates enterprise-wide critical business rules."
在企业 java 和 jpa 的上下文中:
“表示数据库中维护的持久数据的对象。”在“领域驱动设计”(由 Eric Evans 撰写)的上下文中:
“主要由其身份而不是其属性定义的对象。”在“干净架构”(Robert C. Martin)的上下文中:
“封装企业范围内关键业务规则的对象。”
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.
Jee 和 Jpa 社区主要将实体视为映射到数据库表的对象。这种观点与 DTO 的定义非常接近——这可能是大部分混淆的根源。
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.
然而,在域驱动设计的上下文中,以及 Robert Martins 的观点,实体是业务域的一部分,因此可以而且应该实现行为。