tibo
tibo

Reputation: 5494

@ElementCollection count frequency

I have a "tags" property declared like that :

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

This property represents the tags associated to an Article. Now I want to generate a tag cloud. So I want to count the frequency of each tag.

Is there a simple way to do that? (without creating a Tag object?)

Upvotes: 2

Views: 534

Answers (1)

Steve Ebersole
Steve Ebersole

Reputation: 9443

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];
}

Upvotes: 2

Related Questions