没有使用hibernate获取完整的数据

时间:2016-05-16 07:25:05

标签: java mysql sql hibernate jpa

以下是我的实体结构;

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

通过这样做,我也获得了答案的数据,其中没有评论为零。但是当我使用hibernate和我的HQL

SELECT c FROM Comment c 
RIGHT OUTER JOIN c.answer a 
RIGHT OUTER JOIN a.question q
where q.id = :questionId";

我只收到5行,因为我错过了那些没有评论的答案。请帮助我得到答案。

1 个答案:

答案 0 :(得分:0)

在这种情况下你也可以编写一个命名查询,因为你的sql工作正常。

String sql = "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 = :questionId
              order by a.likes desc";

SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Object.class);
query.setParameter("questionId", id);
List results = query.list();