我有三个Domain类
1.Question
2.Test
3.Category
我与这些域有关系
1.Category has many test and test belong to a category
2.Category has many questions and question belongs to a category.
3.Test has many questions and question can belong to many tests.
我已经写了以下标准,但这并不是我想要的输出。你能告诉我这里我做错了吗。
Session session = getSession();
List<String> tests = getCategoryTestIds(categoryId);
List<Question> questions;
questions = session.createCriteria(Question.class)
.createAlias("tests", "test")
.add(Restrictions.and(
Restrictions.eq("category", new Category(categoryId)),
Restrictions.not(Restrictions.in("test.id", tests))))
.setMaxResults(10)
.addOrder(Order.asc("id"))
.list();
}
我将问题列为空列表。我已经在db中检查了结果。理想情况下,这应该给我一个10个问题对象的列表。
以上标准正在进行
选择属于某个类别且不属于任何测试的所有问题。
Hibernate Query正在关注
select this_.id as id1_3_3_, this_.answer as answer2_3_3_, this_.category_id as categor11_3_3_, this_.explanation as explanat3_3_3_, this_.level as level4_3_3_, this_.option_a as option5_3_3_, this_.option_b as option6_3_3_, this_.option_c as option7_3_3_, this_.option_d as option8_3_3_, this_.question as question9_3_3_, this_.set_id as set10_3_3_, category3_.id as id1_0_0_, category3_.name as name2_0_0_, tests4_.question_id as question1_3_, test1_.id as test2_6_, test1_.id as id1_5_1_, test1_.category_id as category3_5_1_, test1_.name as name2_5_1_, category6_.id as id1_0_2_, category6_.name as name2_0_2_ from question this_ left outer join category category3_ on this_.category_id=category3_.id inner join test_question tests4_ on this_.id=tests4_.question_id inner join test test1_ on tests4_.test_id=test1_.id left outer join category category6_ on test1_.category_id=category6_.id where (this_.category_id=? and not (test1_.id in (?, ?))) order by this_.id asc limit ?
我已经尝试过这个查询并获得了以下hibernate sql查询
questions = session.createCriteria(Question.class)
.createAlias("tests", "test")
.add(Restrictions.eq("category.id", categoryId))
.add(Restrictions.isNull("tests"))
.setMaxResults(10)
.addOrder(Order.asc("id"))
.list();
question this_
left outer join category category3_ on this_.category_id=category3_.id
inner join test_question tests4_ on this_.id=tests4_.question_id
inner join test test1_ on tests4_.test_id=test1_.id
left outer join category category6_ on test1_.category_id=category6_.id
where this_.category_id=? and this_.id is null
order by this_.id asc limit ?
错误在where this_.category_id=? and this_.id is null
附近,它正在检查问题的ID而不是测试