There is an entity (1) that has one-to-many associations to other entities (2).

I want to transfer only DTO (1) to the view level, but the fact is that the collections inside this DTO (1) are also DTO (2), and Hibernate, as far as I know, does not know how to load one to many right away as DTO, Because of what, I think, I have only two ways left:

  1. In HQL, write join fetch and then redo all collections through the loop from Entity to DTO

  2. Create a method that will load the entity (1) of its entity (2) already in the form of a DTO by id, but then I have to run to the database once again

Which of these methods is more correct?

    1 answer 1

    A more correct method cannot be determined if the class architecture is not taken into account. There are many ways to initialize objects. If you have a construction, for example, then you can use @OneToMany(fetch = FetchType.EAGER) .

    In all cases, Hibernate will pull data from a subordinate table, either by joining tables by key or by additional queries by key. The second is more or less reasonable, since only one key value is used.