Hibernate select elements of collection with a given property
<p>Entity <code>Project</code> has a collection property <code>contributors</code> mapped with a <code>@OneToMany</code> relationship to entity <code>User</code></p>

<pre><code>@Entity
@Table( name = "projects" )
public class Project {
    ...

    @OneToMany
    @JoinTable(name = "project_contributors")
    private List&lt;User&gt; contributors = new ArrayList&lt;User&gt;();

    ...
}
</code></pre>

<p>I then need to check if <code>contributors</code> already has a <code>user</code> with <code>id</code> <code>contributorId</code> before adding it. I am trying with HQL query, but I am clearly quite innept.</p>

<p>What I am trying:</p>

<pre><code>Query query = session.createQuery(
        "select p.contributors from Project p where p.id = :pId and p.contributors.id = :cId"
    );

query.setParameter("pId", projectId);
query.setParameter("cId", contributorId);

@SuppressWarnings("unchecked")
List&lt;User&gt; res = (List&lt;User&gt;) query.list();
</code></pre>

<p>But it gives the error </p>

<pre><code>illegal attempt to dereference collection [project0_.id.contributors] with element property reference [id]
</code></pre>

<p>Is there a good samaritan that would like to give me a little push?</p>

<p>another try I made is</p>

<pre><code>"select p.contributors as c from Project p where p.id = :pId and c.id = :cId"
</code></pre>

<p>but nothing.</p>