3

I know what is DTO: An object that carries data between processes in order to reduce the number of method calls. and I know what is lazy-loading in hibernate.

I read this sentences in "Full Stack Development with JHipster" book : JHipster uses DTO (Data Transfer Object) and VM (View Model) on the server side. DTOs are for transferring data from the service layer to and from the resource layer. They break the Hibernate transactions and avoids further lazy loading from being triggered by the resource layer.

I don't understand the relationship between DTO and lazy loading.

3 Answers 3

5

Lazy loading is for entities, not DTOs.

A JPA entity can be represented as a POJO or a Proxy.

Using EntityManager.find gives you a POJO:

Post post = entityManager.find(Post.class, postId);

While the EtityManager.getReference method gives you a Proxy:

Post post = entityManager.getReference(Post.class, postId);

The POJO has its basic properties initialized because a SELECT statement was executed to fetch the entity. The Proxy does not hit the database upon creation. Only the id is set based on the provided entity identifier. Only you access the Proxy properties, a SELECT statement will be executed.

Proxies are also used for collections (e.g. @OneToMany or @ManyToMany) which are using the FetchType.LAZY strategy by default. Once you access the LAZY collection, a SELECT statement will be executed to fetch the associated collection.

Now, a DTO is based on a projection, hence a SELECT statement is executed prior to populating the DTO. For this purpose, you can say that the DTO is eagerly loaded every time.

DTOs are much more efficient than entities for read-only projections because you load just the table columns that you explicitly requested.

4
  • Should i use DTOs or read-only Entities for read-only operations?
    – Arash
    Commented Dec 18, 2019 at 5:26
  • 1
    It depends on how much data you need to be fetched. If you need the entire entity, use that. If you only need a subset of that entity, then use a DTO. Commented Dec 18, 2019 at 5:35
  • Thanks Vlad. Is it legal to access an entity in presentation layer?
    – Arash
    Commented Dec 18, 2019 at 5:39
  • 1
    Since the UI usually operated on JSON data from the server, it's just a matter whether it's fine to transform the entity to JSON. So, if the entity doesn't have proxies, it's not that much different than serializing a DTO to JSON. Proxies usually complicate this process if they are uninitialized and the mapper tries to access them. Commented Dec 18, 2019 at 6:11
2

If you pass Hibernate managed persistent entities to the resource layer with LazyLoading enabled, the resource layer will invoke get-methods in order to read their properties some of which are possibly not initialized at that time. Hibernate will then lazily load the data of that particular property from the persisntet store, sending an SQL statement (possibly each time starting and committing a new transaction) and waiting for response which is quite slow. If this happens hundred of times, each time having to initialize another property, each time triggering a lazy load, the user has to wait...

If you prepare a DTO in your service layer, containing all the data relevant to the resource layer, read access to that DTO is highly efficient without the risk of hitting the database for any get-method invokation as everything relevant is in memory already. No subsequent lazy load will be triggered.

1

In Simple terms: If you call getter of a lazy loading marked field, Hibernate will make a db query for that data.

This is one of the reasons you should not return Entities as backend response... any JSON conversion(Serialization)/getter call will trigger unwanted loading of the data.

whereas a DTO is made specifically to transfer data as per use and you create DTO from these Entities (Multiple DTO can be created from one Entity) and pick only required data fields

Eg. User entity: has user details and list of friends UserDetailsDTO: would only require the details and dont need 1000's of friends mapped to the user... and their friends... UserFriendsDTO: we can selectively get the friends names or ids for this DTO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.