To get rid of lazy initialization exception you should not call for lazy collection when you operate with detached object.
From my opinion, best approach is to use DTO, and not entity. In this case you can explicitly set fields which you want to use. As usual it's enough. No need to worry that something like jackson ObjectMapper
, or hashCode
generated by Lombok will call your methods implicitly.
For some specific cases you can use @EntityGrpaph
annotation, which allow you to make eager
load even if you have fetchType=lazy
in your entity.