将SQL查询重写为HQL [英] rewrite SQL query to HQL
本文介绍了将SQL查询重写为HQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有SQL查询:
SELECT
tag.id,
tag.description,
tag.`name`,
COUNT(question_has_tag.question_id)
FROM
question_has_tag
INNER JOIN
question
ON
question.id = question_has_tag.question_id
INNER JOIN
tag
ON
question_has_tag.tag_id = tag.id
GROUP BY
tag.id
一切正常.但是我需要将其重写为HQL,有人知道吗?我试过了,但是没用:
All works good. But I need to rewrite it to HQL, some one have any idea? I tried this, but it doesn't work:
list = entityManager.createQuery("SELECT " +
"t.id, " +
"t.description, " +
"t.name, " +
"COUNT(q.id) " +
"FROM Tag t INNER JOIN Question q ON t.id = q.id " +
"GROUP BY t.id").getResultList();
对不起,不是所有信息,我有3个表标签,问题和question_has_tag(由hibernate自动创建)标签,并且问题与ManyToMany有关联
Sorry it not all information, I've 3 table tag, question and question_has_tag (auto created by hibernate) tag and question has ManyToMany relations
@ManyToMany(mappedBy = "tags", fetch = FetchType.LAZY)
private List<Question> questions;
和
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "question_has_tag",
joinColumns = @JoinColumn(name = "question_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags;
推荐答案
尝试一下
list = entityManager.createQuery("SELECT " +
"t.id, " +
"t.description, " +
"t.name, " +
"COUNT(q.id) " +
"FROM Tag t JOIN t.questions q" +
"GROUP BY t.id").getResultList();
这篇关于将SQL查询重写为HQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文