There is a condition in the criteria

Consider this request:


select user_id
from user_role a
where a.role_id = -1
      and not exists (select 1 
                      from user_role b 
                      where b.role_id != a.role_id 
                            and b.user_id = a.user_id);

      

I am trying to recreate it using gorm criteria. Is it possible? How do I add an offer condition?

UPDATE

I tried to solve this way:

UserRole.createCriteria().list{
  eq('role', roleObj)
  createAlias('user', 'u')
  not{
    inList('u.id', {
      not {
        eq('role', roleObj)
      }
      projections {
       property 'user.id'
      }
    })
  }
}

      

Still not working. I am getting this error while executing it:

DUMMY$_closure1_closure2_closure3_closure4 cannot be cast to java.lang.Long

      

I don't understand the error message. Internal criteria return a list of ids, and if I replace the internal criteria with a list of lengths, it works. Any hint?

Thank you in advance

+3


source to share


1 answer


Not tested: Try Sql constraint

 UserRole.createCriteria().list {
  and{
    eq('roleId',-1)
    sqlRestriction(" not exists(select 1 
                        from UserRole  b 
                        where ............ 
                              )")
  }

      



}

- Hopefully you named your domain as UserRole and the column name as roleId.

0


source







All Articles