I am currently working on a grails project right now where I needed to combine the result of my query in a list. The problem is, there are instances that the result of my query returns equal values, which causes an error message: A different object with the same identifier value was already associated with the session
Here’s my code:
List permissions = []
cmd?.role.each{ role ->
permissions.add(RolePermission.executeQuery("select distinct rp.permission from RolePermission rp where rp.role = ?",[Role.get(role.toLong())]))
}
The object role here may contain two different role names that in some instances the permissions present in these role names are the same.
How will I modify my query in such a way that I can only get the unique values from the result ? I’ve tried using distinct, but it didn’t work.
Please help!
Thanks!
I’m not sure if this is what you’re trying to do, but from your description I think this is it. What the below line does is iterate over your passed in roles (I assume these are role id’s from the syntax) and for each role id it finds a RolePermission for the Role. Each permission found is added as a list to permissions. So, at the end you should have a list of RolePermissions.
Now, what I’m not understanding is the distinctness your looking for. Are you saying that a Role can return more than one RolePermission and your trying to make sure that the returned RolePermissions are unique in the final list? If so you could return the list as a set (i.e. return permissions as Set). Please let me know where my understanding falls short.
Alternatively, you could use a criteria:
I didn’t run this through the compiler, but it should work.