0

我想在 Grails 中创建以下语句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是连接表。

更新:

我的Person课是:

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;
}
}

我要建立的标准是在方法查询中。当我尝试执行它时,我得到groovy.lang.MissingMethodException: No signature of method: grails.orm.HibernateCriteriaBuilder.authorities() is applicable for argument types ...

4

2 回答 2

2

这取决于您如何准确定义关联,但看起来像:

Person.withCriteria {
    authorities {
        'in'('authority', 'ROLE_ADMIN')
    }
}
于 2012-10-09T18:04:17.197 回答
1

也许,我误读了,但看起来你authority没有命名你的协会authorities

尝试:

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

您没有列出您的Authority域类,param.authorities 是权威对象列表还是权威的“名称”?如果是Authority对象,不用join试试(这个我没试过,但我认为是正确的):

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

如果它是一个名称列表(并且权威有一个name字段):

if(params.authorities){
    authority{
        'in'('name', params.authorities)
    }
}
于 2016-10-13T17:47:25.530 回答