hql - Hibernate select elements of collection with a given property -


entity project has collection property contributors mapped @onetomany relationship entity user

@entity @table( name = "projects" ) public class project {     ...      @onetomany     @jointable(name = "project_contributors")     private list<user> contributors = new arraylist<user>();      ... } 

i need check if contributors has user id contributorid before adding it. trying hql query, quite innept.

what trying:

query query = session.createquery(         "select p.contributors project p p.id = :pid , p.contributors.id = :cid"     );  query.setparameter("pid", projectid); query.setparameter("cid", contributorid);  @suppresswarnings("unchecked") list<user> res = (list<user>) query.list(); 

but gives error

illegal attempt dereference collection [project0_.id.contributors] element property reference [id] 

is there samaritan give me little push?

another try made is

"select p.contributors c project p p.id = :pid , c.id = :cid" 

but nothing.

contributors collection. such, not have attribute named id.

idis attribute of elements of collection.

you can fix issue joining collection instead of dereferencing it:

select p    project pj    join pj.contributors  p   pj.id       = :pid    , p.id     = :cid 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -