【发布时间】:2016-05-16 07:25:05
【问题描述】:
以下是我的实体结构;
class Question{}
class Answer {
@ManyToOne
Question q;
}
class Comment {
@ManyToOne
Answer a;
}
为了通过 questionId 获取问题的所有详细信息,我使用查询得到以下结果
select * from Comment c
right join Answer a on c.answer_id = a.id
right join Question q on a.question_id = q.id
where q.id = 27
order by a.likes desc;
[有六行]
'10', '2016-05-16 11:41:22', '0', 'I use pink so it should be pink.', '0', '2016-05-16 11:43:43', '0', '0', '1', NULL, '16', '159', NULL, NULL, '16', '2', '2016-05-16 11:33:51', '0', 'I use black so it should be black', '2', '2016-05-16 11:37:24', '0', '1', '2016-05-16 12:10:32', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
'11', '2016-05-16 11:41:45', '0', 'What about CYAN. :p', '0', '2016-05-16 11:43:43', '0', '0', '1', NULL, '16', '159', NULL, NULL, '16', '2', '2016-05-16 11:33:51', '0', 'I use black so it should be black', '2', '2016-05-16 11:37:24', '0', '1', '2016-05-16 12:10:32', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
'8', '2016-05-16 11:39:00', '0', 'And those factors are??', '0', '2016-05-16 11:43:42', '0', '0', '1', NULL, '15', '159', NULL, NULL, '15', '2', '2016-05-16 11:33:28', '0', 'This depends on many factors. Start noticing different mobile phones.', '1', '2016-05-16 11:37:24', '0', '1', '2016-05-16 12:10:03', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
'9', '2016-05-16 11:40:28', '0', 'Color of the SUN', '0', '2016-05-16 11:43:43', '0', '0', '1', NULL, '15', '159', '8', NULL, '15', '2', '2016-05-16 11:33:28', '0', 'This depends on many factors. Start noticing different mobile phones.', '1', '2016-05-16 11:37:24', '0', '1', '2016-05-16 12:10:03', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '17', '0', '2016-05-16 11:34:55', '0', 'Not every company manufactures yellow mobile phone.', '1', '2016-05-16 11:37:24', '0', '1', '2016-05-16 12:10:18', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
'12', '2016-05-16 11:42:15', '0', 'Thought Well', '0', '2016-05-16 11:43:43', '0', '0', '1', NULL, '18', '159', NULL, NULL, '18', '1', '2016-05-16 11:35:32', '0', 'It is similar to color of the cars you see on the road.', '0', '2016-05-16 11:37:24', '0', '1', '2016-05-16 11:43:27', '146', NULL, '27', NULL, '27', '4', '2016-05-16 11:30:28', '2016-05-16 11:31:01', '0', 'What is most preferred color', '9', '1', NULL, '2016-05-16 12:10:32', '146', NULL, NULL
因此,通过这样做,我还获得了 cmets 数为零的答案的数据。但是当我在 HQL 中使用休眠时
SELECT c FROM Comment c
RIGHT OUTER JOIN c.answer a
RIGHT OUTER JOIN a.question q
where q.id = :questionId";
我只得到 5 行,因此我错过了那些没有 cmets 的答案。请帮我解答。
【问题讨论】:
标签: java mysql sql hibernate jpa