我们说我有以下两个班级:
@Entity
class Article {
@Id
int id;
@ManyToMany(mappedBy="articles")
List<Tag> tags;
}
@Entity
@NamedQueries({
@NamedQuery(name = "Tag.trends", query = "SELECT t FROM Tag t") // Some magic stuff here..
})
class Tag {
@Id
String name;
@ManyToMany
List<Articles> articles;
}
现在我想创建一个命名查询来选择最常用的标签。
因此,这需要加入,计数和订单。但我似乎无法解决这个问题。
问题: 如何选择最常用的标签?
例如,如果以下文章具有以下标记:
Article: Tags
0: A, B
1: A
2: C
3: A, C
输出
A // 3 times
C // 2 times
B // 1 time
答案 0 :(得分:2)
您可以使用
之类的东西来实现您想要的效果SELECT t FROM Tag t ORDER BY SIZE(t.articles) ASC
这将为您提供按文章数量排序的标签列表。
答案 1 :(得分:1)
在这些问题上向后退一步。从SQL查询开始:
select
count(t.name), t.name
from article a
join article_tags ats
join tag t
where
a.id=ats.article_id
and ats.tag_id = t.id
group by
t.name
order by
count(t.name)
desc;
然后JPQL应该或多或少地遵循:
List<Tuple> t = em.createQuery("select t.name as name, count(t.name) as count from Article a join a.tags t group by (t.name) order by count(t.name) desc", Tuple.class).getResultList();
t.stream().forEach((tp)-> {
System.out.println("Count of " + tp.get("name") + " = " + tp.get("count"));
});
这给了我以下输出:
Hibernate: select tag2_.name as col_0_0_, count(tag2_.name) as col_1_0_ from Article article0_ inner join Tag_Article tags1_ on article0_.id=tags1_.articles_id inner join Tag tag2_ on tags1_.tags_id=tag2_.id group by tag2_.name order by count(tag2_.name) desc
Count of A = 2
Count of B = 1
Count of C = 1
更新:如果你想要一个标签列表,只需将元组的第一个元素更改为标签。当然,如果你愿意,你可以忽略计数:
List<Tag> t = em.createQuery("select t from Article a join a.tags t group by (t.name) order by count(t.name) desc", Tag.class).getResultList();
t.stream().forEach((tag)-> {
System.out.println("Tag = " + tag.getName());
});
获得以下输出:
Hibernate: select tag2_.id as id1_1_, tag2_.name as name2_1_ from Article article0_ inner join Tag_Article tags1_ on article0_.id=tags1_.articles_id inner join Tag tag2_ on tags1_.tags_id=tag2_.id group by tag2_.name order by count(tag2_.name) desc
Tag = A
Tag = B
Tag = C