Ekansh Rastogi
Ekansh Rastogi

Reputation: 2526

How to get the list of records in Hibernate using Criteria Query

I have three classes

  1. Category{}
  2. Topic{ @ManytoOne Catgoery category

    @ManyToMany Questions }

  3. Question{ @ManytoMany Topic }

So if i have to find all the questions that belong to a Category. I need to first find 1. All the Topics of the Category 2. Then find all the Questions that belong to this Category list of step 1

Can any one tell me how to write this query

I have tried many things, however could not get it right.

List<Topic> topicList = getSession()
                .createCriteria(Topic.class)
                .add(Restrictions.eq("category", new Category(id)))
                .list();

        System.out.println(">>>>>>>>>>>>>>>>>size="+topicList.size());
        return getSession().createCriteria(Question.class)
                .createAlias("topics", "topic")
                .add(Restrictions.in("topic", topicList))
                .list();

I get the following output

>>>>>>>>>>>>>>>>>size=8
org.hibernate.QueryException: could not resolve property: topic of: com.ekiras.domain.Question
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1985)
    at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61)
    at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1960)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:523)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:538)
    at org.hibernate.criterion.InExpression.toSqlString(InExpression.java:51)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:123)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:92)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:95)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1643)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
    at com.ekiras.dao.QuestionDAO.findByCategoryId(QuestionDAO.java:35)
    at com.ekiras.service.QuestionService.findByCategory(QuestionService.java:22)
    at com.ekiras.controller.QuestionController.listCategoryQuestions(QuestionController.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Upvotes: 1

Views: 630

Answers (1)

Viraj Nalawade
Viraj Nalawade

Reputation: 3227

I could be wrong but I think Hibernate message is pretty clear it saying your Question class does not have "topic" as filed member . Can you cross check for the name and spelling in your Question class? I say this because I dint find anything else wrong here unless we get to see entity classes and I have faced such issue and it was case with me.

Upvotes: 1

Related Questions