JPA实体和/ VS的DTO [英] JPA Entities and/vs DTOs

查看:850
本文介绍了JPA实体和/ VS的DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一般的想法,以帮助决定何时使用DTO,当在这些情况下使用实体?


  1. UI /服务器端java调用服务。如果它接收/发送实体或DTO的?

  2. Web服务调用服务。如果接受服务的实体或DTO的?

我喜欢读code,大约经过实体:


  1. 简单绕过,没有需要映射到的DTO

  2. 不需要额外的类

  3. 关系的其他实体已经定义,所以不需要相关的DTO合并成一个DTO

  4. 刚的POJO

但有大约DTO映射到一个实体是安全的,因为它是一个合同,该实体可以换任何形式和DTO将保持相同的参数。例如,像实体具有一个字段名称和DTO也有一个字段名。后来,如果需求发生变化时,数据库表的更改,实体也可以改变,改名字进入firstName和lastName。但DTO仍会有一个字段名,这是的firstName + lastName的。

所以这里的用的DTO的优点的列表:


  1. 从受理的DTO的code的角度向后兼容

DTO的我能想到的利弊:


  1. 需要定义DTO类和映射(可能使用推土机)

  2. 程序员必须分析时使用DTO和实体,我的意思传递DTO为每个方法是一个烂摊子

  3. 实体转换开销的DTO,反之亦然

  4. IM仍然不确定如何把它们映射一个一对多的关系。在JPA,我们可以偷懒初始化这一点,但传递DTO的时候,我应该初始化这个与否。不久,DTO的不能有延迟初始化代理,只包含的值。

请分享你的想法。

感谢您!

下面是来自不同地方的一些报价

亲DTO


  

实体类作为DTO的再利用
  看起来凌乱。的公共API
  类(包括公共注释
  方法)不再明确界定
  的合同目的是
  presenting。类将结束与
  是当只有相关的方法
  类被用作一个DTO和
  一些方法,将只
  正在使用的类时relavent
  作为一个实体。担忧会不会
  分离干净,事情会
  更紧密耦合。对我来说,这是一个
  更重要的设计考虑
  然后设法节省数
  类文件的创建。


<一个href=\"http://www.$c$cranch.com/t/462706/java-Architect-SCEA/certification/Pass-JPA-entities-across-tiers\">pro实体:


  

绝对不是!


  
  

JPA实体映射​​到一个数据库,
  但它们不是捆绑到数据库。
  如果数据库更改,更改
  的映射,而不是对象。该
  对象保持不变。这就是
  整点!



解决方案

我会去的DTO选项,原因如下:


  • 服务接口应该是数据库的独立,在一个变化不应该总是要求对方改变。

  • 您正在你的服务将始终由Java客户端调用一个假设

  • 使用延迟加载时,对象是Web服务调用的阿瑟赛德无法正常工作。

What is the general idea to help deciding when to use DTO and when to use Entity in these cases ?

  1. UI / server side java calling the services. Should it get / send entities or DTOs ?
  2. Web service calling the services. Should the services accept entities or DTOs ?

I like reading the code that pass entities around :

  1. simpler to pass around, no need to map to DTOs
  2. dont need extra classes
  3. relations to other entities are already defined, so dont need combine related DTOs into one DTO
  4. just POJOs

But there are arguments about DTO that maps to an entity is safer, because it's a contract, and the entity can change to whatever form, and the DTO will stay the same. For example, like the entity has a field name, and the DTO also has a field name. Later on, if the requirement changes, the database table changes, the entity can change also, changing name into firstName and lastName. But the DTO will still have a field name, which is firstName + lastName.

So here's the list of the pros of using DTOs :

  1. backward compatible from the view point of the code that accepts the DTOs

The cons of DTO i can think of is :

  1. have to define the DTO classes and the mapping (perhaps using dozer)
  2. the programmers would have to analyze when to use DTO and entity, i mean passing DTO for every methods is a mess
  3. overhead of conversion of entities to DTOs and vice versa
  4. im still unsure about the one-to-many relationship on how to map them. In JPA we can lazy initialize this, but when passing in DTO, should i initialize this or not. Shortly, DTOs cant have lazy initialized proxies, only contains values.

Please share you thoughts ..

Thank you !

Here are some quotes from different places

pro dto :

Reuse of the entity class as a DTO seems messy. The public API of the class (including annotations on public methods) no longer clearly defines the purpose of the contract it is presenting. The class will end up with methods that are only relevant when the class is being used as a DTO and some methods that will only be relavent when the class is being used as an entity. Concerns will not be cleanly separated and things will be more tightly coupled. To me that is a more important design consideration then trying to save on the number of class files created.

pro entity :

Absolutely NOT!!!

JPA entities are mapped to a database, but they are not 'tied' to a database. If the database changes, you change the mappings, not the objects. The objects stay the same. That's the whole point!

解决方案

I would go for the DTO option for the following reasons:

  • The service interface should be independant of the database, a change in one should not always require a change in the other.
  • You are making an assumption that your services will always be called by a Java client
  • Using lazy loading when the object is on the otherside of a web service call does not work well.

这篇关于JPA实体和/ VS的DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆