LazyInitializationException

To avoid the LazyInitializationException, you can either make sure to access the lazily loaded property or collection while still within the Hibernate session or transaction, or you can configure Hibernate to eagerly load the property or collection instead of lazily loading it.

Vlad Mihalcea - The best way to handle the LazyInitializationException

Projection

Projection refers to the query of a database query by selecting only a few columns. Returns only the values of the selected columns by extracting only the desired columns from the query results. This can speed up query execution.

Using DTO:

data class PostCommentDto(
    val id: Long,
    val review: String,
    val title: String
)
List<PostCommentDTO> comments = doInJPA(entityManager -> {
    return entityManager.createQuery(
        "select new " +
        "   PostCommentDto(" +
        "       pc.id, pc.review, p.title" +
        "   ) " +
        "from PostComment pc " +
        "join pc.post p " +
        "where pc.review = :review", PostCommentDTO.class)
    .setParameter("review", review)
    .getResultList();
});

Using Interface:

public interface PostOfComments {
    Long getId();

    String getReview();

    String getTitle();
}
List<PostOfComments> comments ...

ADMIN 처럼 통계성 쿼리를 뽑아내야 하는 경우에 유용하게 쓰일 수 있다. 혹은 CQRS 를 구현할 때도 유용하게 쓰일 것 같다.