[英]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
.这会急切地获取Person
的Region
。 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
,进而获取其他Person
的Region
,并且一直向下递归。
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.