mat_boy
mat_boy

Reputation: 13666

QueryDSL to get any entities in a collection of another entity

I'm using JPA with Hibernate and QueryDSL (v.4.0.5). I have this entity:

package com.test.model.entity;

@Entity
public class Article {
    @Id
    private Long id;

    @ManyToMany(fetch = LAZY, cascade = DETACH)
    private Set<Tag> tags;
}

How can I find all the articles matching a given set of Tags? I think I should start as follows:

public BooleanExpression hasTag(Set<Tag> tags){
    final QArticle article = QArticle.article;
    return article.tags.any().eqAny(ce);
}

where ce should be a CollectionExpression. Clearly I have no idea how to set this.

Any solution?

Upvotes: 4

Views: 4939

Answers (1)

Timo Westk&#228;mper
Timo Westk&#228;mper

Reputation: 22180

Did you try

public BooleanExpression hasTag(Set<Tag> tags){
    QArticle article = QArticle.article;
    return article.tags.any().in(tags);
}

Upvotes: 5

Related Questions