java - 使用 hibernate 无法获取完整数据

标签 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 行,因此我错过了那些没有评论的答案。请帮助我得到答案。

最佳答案

在这种情况下,您也可以很好地编写命名查询,因为您的 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();

关于java - 使用 hibernate 无法获取完整数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248754/

相关文章:

左连接时MySQL错误的计数值

php - 计算 MySQL 结果之间的平均值

php - 在表的字段上设置超链接

mysql - 如何组合 GROUP BY、ORDER BY 和 HAVING

Java 和 Kotlin 组合构建错误(Duplicate Zip Entry)

java - 如果图像被篡改了一些附加内容,如何用Java从图像中删除这些附加内容?

c# - 删除 SQL 数据库中不存在的图像

sql - 如何显示每个月的利润总额,当oracle中该月没有记录时显示null

java - Thymeleaf 中的奇怪错误替换为变量

java - 使用 Java 将文本从一个文件复制到另一个文件