0

I just want to retrieve list of data but by using below code why i get class cast exception...

public List<TbDiscussionForumAnswere> findTbDiscussionForumAnswerebyforumQuestionId(Integer forumQuestionId) {
session = sessionFactory.getCurrentSession();
TbDiscussionForumAnswere TbDiscussionForumAnswere = (TbDiscussionForumAnswere)session.createCriteria(TbDiscussionForumAnswere.class)                .add(Restrictions.eq("forumQuestionId.forumQuestionId", forumQuestionId));
return (List<TbDiscussionForumAnswere>) TbDiscussionForumAnswere.getTbDiscussionForumAnswereCollection();
}

above method gives me below exception

java.lang.ClassCastException: org.hibernate.internal.CriteriaImpl cannot be cast to com.medikm.entity.TbDiscussionForumAnswere

Thank u

1
  • did you check our answers? please accept an answer. thnx! Commented Jul 13, 2016 at 11:28

3 Answers 3

1

you are creating a Criteria object not a TbDiscussionForumAnswere object. this is a correct error message. You use this Criteria class to execute a query. you should cast it to Criteria class

Sign up to request clarification or add additional context in comments.

Comments

1

session.createCriteria(TbDiscussionForumAnswere.class).add(Restrictions.eq("forumQuestionId.forumQuestionId", forumQuestionId)); Returns an Critera not and object TbDiscussionForumAnswere

so you have to Change the line to:

Critera critera  = (Critera)session.createCriteria(TbDiscussionForumAnswere.class).add(Restrictions.eq("forumQuestionId.forumQuestionId", forumQuestionId));

Comments

0

below code i use and this works fine for me thank you all for your answers

List<TbDiscussionForumAnswere> tbDiscussionForumAnswereList = session.createCriteria(TbDiscussionForumAnswere.class)
.add(Restrictions.eq("forumQuestionId.forumQuestionId",forumQuestionId))
.setFetchMode("forumQuestionId", FetchMode.LAZY)
.list();

2 Comments

please accept at least one answer so as the question to be considered closed.
also consider upvoting the answers that helped you with your problem. this is the "reward" for the people that spent sometime helping you! regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.