我有三个班级
主题{ @ManytoOne Catgoery类别
@ManyToMany问题 }
所以,如果我必须找到所有属于类别的问题。我需要先找到 1.所有类别的主题 2.然后找到属于步骤1的此类别列表的所有问题
任何人都可以告诉我如何编写此查询
我尝试了很多东西,但无法做到。
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();
我得到以下输出
>>>>>>>>>>>>>>>>>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)
答案 0 :(得分:1)
我可能错了,但我认为Hibernate的消息非常清楚,它说你的问题类没有&#34;主题&#34;作为提交的成员。您可以在问题类中交叉检查姓名和拼写吗?我这样说是因为我在这里找不到任何其他错误,除非我们看到实体类并且我遇到了这样的问题而且我的情况也是如此。