Beispiel #1
0
  @SuppressWarnings("unchecked")
  public List<PostAndReply> getPostsAndReplies(int forumId) {
    // TODO Auto-generated method stub
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    List<Post> postArr = new ArrayList<Post>();
    List<PostAndReply> prArray = new ArrayList<PostAndReply>();

    try {
      tx = session.beginTransaction();
      Query query =
          session.createQuery("from Post where forumId = :forumId order by createdDate desc");
      query.setParameter("forumId", forumId);
      postArr = query.list();
      System.out.println("No of posts.." + postArr.size());
      for (Post post : postArr) {
        int postId = post.getPostId();
        System.out.println("Post id----" + postId);
        List<Reply> replies = getRepliesToPost(postId);
        User user = getUser(post.getCreatedBy());
        String postCreatorName = user.getFirstname() + ' ' + user.getLastname();
        PostAndReply pr = new PostAndReply();
        pr.setPost(post);
        pr.setReplies(replies);
        pr.setPostCreatorName(postCreatorName);
        prArray.add(pr);
        // System.out.println("Post - Description----"+post.getDescription());
        // System.out.println("No of Replies----"+replies.size());
      }
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
        e.printStackTrace();
      }
    } finally {
      session.close();
    }
    return prArray;
  }