Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22635

Join statement in hibernate criteria builder in grails

I would like to create the following statement in Grails with HibernateCriteriaBuilder:

SELECT * FROM person p 
  JOIN person_authority pa ON p.id=pa.person_id 
  JOIN authority a ON pa.authority_id=a.id 
  WHERE a.authority IN ('ROLE_ADMIN');

person_authority is join table.

Update:

My Person class is:

class Person {

transient springSecurityService

Date dateCreated
Date lastLogin

String username
String password
String email;
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired

static constraints = {
    username blank: false, unique: true
    email blank:false, unique: true, email: true
    password blank: false
    lastLogin nullable:true
}

static mapping = {
    hasMany authority:Authority;
    password column: '`password`'
}

//Set<Authority> getAuthorities() {
//  PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
//}

def beforeInsert() {
    encodePassword()
}

def beforeUpdate() {
    if (isDirty('password')) {
        encodePassword()
    }
}

protected void encodePassword() {
    password = springSecurityService.encodePassword(password)
}

public static def query(Map params = [:]){

    def rows = withCriteria(max:params.max,offset:params.offset){
        if(params.authorities){
            authorities{
                'in'('authority', params.authorities)
            }
        }
        order(params.column?:"id",params.order?:"asc")
        if(params.id){
            idEq (params.id as Long);
        }
        if(params.username){
            ilike "username","%"+params.username+"%"
        }
        if(params.email){
            ilike "email","%"+params.email+"%"
        }
        if(params.accountLocked){
            eq "accountLocked",Boolean.valueOf(params.accountLocked)
        }
        if(params.enabled){
            eq "enabled",Boolean.valueOf(params.enabled)
        }
        if(params.passwordExpired){
            eq "passwordExpired",Boolean.valueOf(params.passwordExpired)
        }
    }
    return rows;
}
}

Criteria I want to build is in method query. When I try to execute it I get groovy.lang.MissingMethodException: No signature of method: grails.orm.HibernateCriteriaBuilder.authorities() is applicable for argument types ...

Upvotes: 0

Views: 1915

Answers (2)

Trebla
Trebla

Reputation: 1174

Perhaps, I'm misreading, but it looks like you named your association authority not authorities

try:

def rows = withCriteria(max:params.max,offset:params.offset){
    if(params.authorities){
        authority{
            'in'('authority', params.authorities)
        }
    }

You didn't list your Authority domain class, is the param.authorities a list of Authority objects or maybe the "name" of the Authority? If it's the Authority object, try it without the join (I haven't tried this, but I think it's correct):

if(params.authorities) {
  'in'('authority', parmas.authorities)
}

If it's a list of names (and Authority has a name field):

if(params.authorities){
    authority{
        'in'('name', params.authorities)
    }
}

Upvotes: 1

Sergei Shushkevich
Sergei Shushkevich

Reputation: 1376

It depends on how exactly you've defined association, but will look like:

Person.withCriteria {
    authorities {
        'in'('authority', 'ROLE_ADMIN')
    }
}

Upvotes: 2

Related Questions