简体   繁体   English

带有 JPA 休眠的 DTO 模式

[英]DTO pattern with JPA hibernate

I'm following this tutorial about DTO and checking the way to convert Entity to DTO.我正在关注有关 DTO 的本教程并检查将实体转换为 DTO 的方法。 I wonder what's the meaning of lazy relationship, if in the moment of converting it to DTO, I need to get the data in order to set it into the DTO class?我想知道懒惰关系是什么意思,如果在将其转换为DTO的那一刻,我需要获取数据才能将其设置到DTO类中? I always need to get the data first, so does it matter if I set it lazy or eager?我总是需要先获取数据,那么我设置它是lazy 还是eager 有关系吗?

In lazy loading child class values are loaded only when getter of child classes are explicitly called.在延迟加载中,仅当显式调用子类的 getter 时才加载子类值。 But for eager loading, while invoking parent all its child class have value already loaded in it.但是对于急切加载,在调用父类时,它的所有子类都已经加载了值。 Eager loading is slower and consumes more memory compared to lazy loading.与延迟加载相比,快速加载速度更慢并且消耗更多内存。

I suggest you move your point of view out of "what will my DTO need", as this clouds the judgment.我建议你把你的观点从“我的 DTO 需要什么”上移开,因为这会影响判断。

Eager vs. lazy is rather a design tradeoff between "how often will I need the relationships loaded", vs. "how much can it cost to load the relationship" ?渴望与懒惰是“我需要多久加载一次关系”与“加载关系需要多少成本”之间的设计权衡?

Take an extrême example.举个极端的例子。 You have a persistent entity, say a Region .您有一个持久实体,例如Region A Region has a relationships to all people ( Person entity) living in this Region .一个Region与居住在这个Region所有人( Person实体)都有关系。 And people ( Person ) can know each other.并且人们( Person )可以相互认识。

Let's imagine you eager fetch everything in this entity graph, and you have a requirement to load all regions for a screen (not the people, only the regions).假设您eager获取此实体图中的所有内容,并且您需要加载屏幕的所有区域(不是人,只有区域)。

Loading all Region will eagerly load all Person living there.加载所有Region都会热切加载所有Person生活在那里。 But as people know each other and this will be eagerly loaded too, then you load up the entire database into memory just to display regions.但是由于人们彼此认识并且这也将被急切地加载,然后您将整个数据库加载到内存中只是为了显示区域。

Loading the whole database into memory usually is an unacceptable cost.将整个数据库加载到内存中通常是不可接受的成本。

Wait.等待。 There's worse.还有更糟的。

Say I want to edit a single Person .假设我想编辑一个Person So I load it.所以我加载它。 This eagerly fetches the Person 's Region .这会急切地获取PersonRegion Which eagerly fetches all Person instances in this region.它急切地获取该区域中的所有Person实例。 And it also fetches all other Person s this one knows of, which in turn fetches those other Person 's Region , and recursively all the way down.并且它还会获取这个人知道的所有其他Person ,进而获取其他PersonRegion ,并且一直向下递归。

Even for a single Perosn I've loaded the whole database again.即使对于单个Perosn我也再次加载了整个数据库。

Does that mean you should never have eager fetching ?这是否意味着你永远不应该急切地获取? No.不。

What it does mean is : you can put eager fetching when you know that data is often needed together, AND you can control the amount of data the fetching will incur, directly AND indirectly.它的意思是:当您知道经常需要数据时,您可以将预先提取放在一起,并且您可以直接或间接控制提取将产生的数据量。

This is what is at stake : do not load your entire 200 million rows table for a single entity.这就是危险所在:不要为单个实体加载整个 2 亿行表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate:Doman Model to JPA Entity / DTO&Merge()设计模式或最佳实践 - Hibernate: Doman Model to JPA Entity/DTO & Merge() Design pattern or best practice org.hibernate.MappingException:未知实体 - Hibernate,SpringBoot,DTO 模式 - org.hibernate.MappingException: Unknown entity - Hibernate, SpringBoot, DTO pattern Hibernate / JPA版本的并发控制和DTO /更改命令模式 - Hibernate/JPA version concurrency control and DTO/change command patterns JPA/Hibernate + HQL/JPQL:选择带有 BigDecimal 参数的 DTO - JPA/Hibernate + HQL/JPQL: select DTO with BigDecimal parameter 将 JPA 或 Hibernate 投影查询映射到 DTO(数据传输对象) - Mapping JPA or Hibernate projection query to DTO (Data Transfer Object) 这可能吗:JPA/Hibernate query with list property in DTO method - Is this possible: JPA/Hibernate query with list property in DTO method DTO Query with JPA CriteriaQuery 和 Hibernate 使用实体作为 DTO 构造函数创建许多选择 - DTO Query with JPA CriteriaQuery and Hibernate using entity as a DTO constructor creates many selects JPA的模式:从实体生成数据传输对象DTO并将DTO合并到数据库 - Pattern for JPA: Generating Data Transfer Object DTO from Entity and merging DTO to database JPA / Hibernate / Bean验证程序-鉴别器上的@Pattern吗? - JPA/Hibernate/Bean Validator - @Pattern on Discriminator? Java JPA / Hibernate反模式 - Java JPA/Hibernate anti-pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM