【问题标题】:Join statement in hibernate criteria builder in grails在 grails 的休眠标准构建器中加入语句
【发布时间】:2012-10-09 16:42:27
【问题描述】:

我想在 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 ...

【问题讨论】:

  • PersonAuthority.findAllByAuthority(Authority.findByAuthority("ROLE_ADMIN").person
  • 是否可以在 HibernateCriteriaBuilder 中进行此查询,因为我想动态附加一些限制(如 eqId 等)。
  • 您可以将其拆分出来。获取所有具有该角色的人,然后使用您的过滤器。

标签: hibernate grails join


【解决方案1】:

这取决于您定义关联的准确程度,但看起来像:

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

【讨论】:

  • 它没有用。我在第一篇文章中添加了课程来源。我缺少方法异常。 `groovy.lang.MissingMethodException:没有方法签名:grails.orm.HibernateCriteriaBuilder.authorities() 适用于参数类型...
  • 在“mapping”块外定义“authority”关联(最好是复数形式):static hasMany = [authorities: Authority]
【解决方案2】:

也许,我误读了,但您似乎将您的关联命名为 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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多