0

I'm new to Hibernate and I have some problems with my code, I have problem with EAGER fetch with my 4 pojo classes:

- Post POJO:

    @Data
    @Table(name = "post")
    public class Post implements Serializable {
        @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, mappedBy = "postId")
        private List<ImageInPost> imageInPostList;

        @OneToOne(cascade = CascadeType.REMOVE, mappedBy = "post")
        private PostSurvey postSurvey;

        ...another attributes
}
  • ImageInPost POJO:
    @Data
    @Table(name = "image_in_post")
    public class ImageInPost implements Serializable {
        @JsonIgnore
        @JoinColumn(name = "post_id", referencedColumnName = "id")
        @ManyToOne
        private Post postId;
        
        ...another attributes
    }
  • PostSurvey POJO
    @Data
    @Table(name = "post_survey")
    public class PostSurvey implements Serializable {
       @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, mappedBy = "surveyId")
       private List<Question> questions;

       ...another attributes
    }
  • Question POJO
    @Data
    @Table(name = "question")
    public class Question implements Serializable {
        @JsonIgnore
        @JoinColumn(name = "survey_id", referencedColumnName = "id")
        @ManyToOne(optional = false)
        private PostSurvey surveyId;

        ...another attributes
    }`

When I tried to GET the Post, it throw stack overflow error, I asked ChatGPT and it said to me that I can't use fetch EAGER two times.

So I tried to modify my code like this. In this code, I get persistPost with hql code and check if it not null, get the Post Survey. Then if the Post Survey is not null, I will get Question with hql code and set it to the Post Survey. Then I remove fetch = FetchType.EAGER of the List of Question in the PostSurvey POJO

    @Override
    public Optional<Post> retrieve(Integer postId) throws Exception {
        Session s = sessionFactoryBean.getObject().getCurrentSession();
        Post persistPost = (Post) s.get(Post.class, postId);
        if (persistPost == null) {
            throw new Exception("Null post");
        }
        PostSurvey persistPostSurvey = persistPost.getPostSurvey();
        if (persistPostSurvey != null) {
            Query query = s.createQuery("FROM Question WHERE surveyId.id = :postId");
            query.setParameter("postId", postId);
            persistPostSurvey.setQuestions(query.getResultList());
        }
        try {
            return Optional.ofNullable(persistPost);
        } catch (NoResultException e) {
            return Optional.empty();
        }
    }

The above code is worked, but with another function that use criteria query, I have no idea to modify the code below. I'm trying to get the list of Post, then do another query to Sort the Post but I can't do something such as rPost.getPostSurvey() like hql code.

    @Override
    public Optional<List<Post>> loadNewFeed(@RequestParam Map<String, String> params) {
        Session session = sessionFactoryBean.getObject().getCurrentSession();
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Post> criteriaQuery = builder.createQuery(Post.class);

        Root<Post> rPost = criteriaQuery.from(Post.class);
        // I don't know how to modify the code look like the above code from here.

        ...another code that use the rPost to OrderBy and returning with .getResultList()
    }

Appreciate for your help!

Pla
  • 1
  • 1
  • 1

0 Answers0