@Override public List<Forum> getForumsOfaUser(int userId) { // TODO Auto-generated method stub List<Forum> forums = new ArrayList<Forum>(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery("from Subscription where userId = :userId"); query.setString("userId", String.valueOf(userId)); List<Subscription> userSubscribedGroups = query.list(); for (Subscription subsc : userSubscribedGroups) { query = session.createQuery("from Forum where forumId = :forumId"); query.setParameter("forumId", subsc.getForumId()); // add this to a list variable query.uniqueResult(); forums.add((Forum) query.uniqueResult()); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return forums; }
@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; }