gorm - Query many-to-many with createCriteria in Grails -
how query many-to-many using createcriteria? here models,
class role { string name static hasmany = [users: user] } class user { string name string email }
and have 3 tables generated gorm in database,
role role_user user --------------- ------------------------- --------------------------------- |id |name | |role_users_id |user_id | |id |name |email | --------------- ------------------------- --------------------------------- |1 |owner | |1 |1 | |1 |harry |harry@mail.com | |2 |designer| |2 |2 | |2 |hermione |hermione@mail.com| |3 |cleaner | |3 |3 | |3 |ron |ron@mail.com | --------------- ------------------------- ---------------------------------
i want users 'owner' , 'designer' , have use createcriteria
because going use pagination.
using relationships it's hard query based on user table can want following:
list<user> users = [] role.withcriteria { or { eq( "name", "owner") eq( "name", "designer") } }.each { users += it.users }
if willing change schema , add role role
user
, can following:
list<user> users = user.createcriteria().list() { role { eq( "name", "owner") eq( "name", "designer") } }
fyi syntax of createcriteria
same withcriteria
.
Comments
Post a Comment