Q:

org.hibernate.lazyinitializationexception after calling getter to a lazy initialised field

I have a class post with a lazy init field comments:
@Entity
@Table(name = "POSTS")
public class Post {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "post_id",
            unique = true, nullable = false)
    @JsonView(Views.Public.class)
    private Integer postId;

    @Column(name = "POST_BODY", columnDefinition = "text")
    @JsonView(Views.Public.class)
    private String postBody;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "USERNAME")
    private User user;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "post", fetch = FetchType.LAZY)
    private Set<PostComment> comments = new HashSet<>();
}

As I learned from hibernate docs if something is not initialised due to lazy initialisation if you then call its getter method it should be initialised, but when I get my post and try to call getter method for comments, I get an exception.
@GetMapping(path = {"/post/{id}"})
    public ModelAndView showSpecificPost(@PathVariable(value = "id") Integer id) {
        User currentUser = userService.findByUserName(auth.getName());
        Post post = postService.getPostById(id);
        logger.info(post.getComments().size());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("postTemplates/specificPost");

        return modelAndView;
    }

A:

Most likely the transactional is on this method:
Post post = postService.getPostById(id);

Then you try:
logger.info(post.getComments().size());

which is outside of the transaction which is closed at this point and Post is a detached entity at this point.
One of the options for you would be to annotate the controller request-mapping method with @Transactional(readOnly = true). 

