Preserve Hibernate Lazy loading in data transfer object design pattern
<p>I usually work on 3-tier applications using Hibernate in the persistence layer and I take care to not use the domain model classes in the presentation layer. This is why I use the <code>DTO (Data Transfer Object)</code> design pattern. </p>

<p>But I always have a dilemma in my entity-dto mapping. Whether I lose the lazy loading benefict, or I create complexity in the code by introducing filters to call or not the domain model <code>getters</code>.</p>

<hr>

<p><strong>Example :</strong> <em>Consider a DTO UserDto that corresponds to the entity User</em></p>

<pre><code>public UserDto toDto(User entity, OptionList... optionList) {

        if (entity == null) {
            return null;
        }

        UserDto userDto = new UserDto();
        userDto.setId(entity.getId());
        userDto.setFirstname(entity.getFirstname());


        if (optionList.length == 0 || optionList[0].contains(User.class, UserOptionList.AUTHORIZATION)) {
            IGenericEntityDtoConverter&lt;Authorization, AuthorizationDto&gt; authorizationConverter = converterRegistry.getConverter(Authorization.class);

            List&lt;AuthorizationDto&gt; authorizations = new ArrayList&lt;&gt;(authorizationConverter.toListDto(entity.getAuthorizations(), optionList));
            userDto.setAuthorizations(authorizations);

...
}
</code></pre>

<p><code>OptionList</code> is used to filter the mapping and map just what is wanted.</p>

<p>Although the last solution allow lazy loading but it's very heavy because the <code>optionList</code> must be specified in the service layer.</p>

<hr>

<p><strong>Is there any better solution to preserve lazy loading in a DTO design pattern ?</strong></p>