2012-05-31 67 views
2

我宣布一个这样的“标签”性质:@ElementCollection计数频率

@Entity 
public class BlogArticle 
{ 
    [...] 
    @ElementCollection(fetch = FetchType.EAGER) 
    Set<String> tags =new HashSet<String>(); 
    [...] 
} 

此属性表示相关的对物品的标签。现在我想生成一个标签云。所以我想要统计每个标签的频率。

有没有简单的方法来做到这一点? (没有创建标签对象?)

回答

2
String hql = "select t, count(1) from BlogArticle a inner join a.tags t group by t"; 
List tagCounts = createQuery(hql).list(); 

Iterator itr = tagCounts.iterator(); 
while (itr.hasNext()) { 
    Object[] tagCount = (Object[]) itr.next(); 
    String tag = (String) tagCount[0]; 
    Long count = (Long) tagCount[1]; 
} 
+0

太棒了!谢谢。我只需要用count(*)替换count(1)。 – tibo