0

我是 Grails 的新用户。我不知道如何编写投影查询。这是我的代码。请任何人帮助我进行 grails 投影查询。

从加入表中我想找到包含在用户表中的用户名

给定以下示例域:

class User{
transient springSecurityService

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

 static mapping = {
    table 't04t001'
    id              column: 'f_account_id'
    username        column: 'f_username',       length: 10
    password        column: 'f_password',       length: 100
    enabled         column: 'f_account_active'
    accountExpired  column: 'f_account_expired'
    accountLocked   column: 'f_account_locked'
    passwordExpired column: 'f_password_expired'
    version         column: 'f_revision'
 }
}

class Role{

String role
static mapping = {
    table 't04t003'
    id              column : 'f_role_id'
    role            column : 'f_role'
    version         column : 'f_revision'
    cache true
}
}


class UserRole implements Serializable {

    User user
    Role role

    static mapping = {
        table 't04j002'
        id              composite   : ['role', 'user']
        role            column      :'k_role_id'
        user            column      :'k_user_id'
        version false
    }    
    }

我不知道如何建立标准来查找所有用户。我尝试了以下方法:

    def criteria    = UserRole.createCriteria()
    def list        = criteria.list {  
        projections { 
            user{
                ilike('username', 'omar')
            }  
        }            
    }

在控制台模式下,我看到了这个带有消息的查询

Hibernate: 
select
    this_.k_role_id as k1_3406_0_,
    this_.k_user_id as k2_3406_0_ 
from
    t04j002 this_ 
where
    (
        lower(user_alias1_.f_username) like ?
    )

但是,它在“where 子句”中显示未知列“user_alias1_.f_username”。

但我无法弄清楚这个(user_alias1_)别名

4

1 回答 1

0

我不太清楚你想从哪个表中检索什么。因此,根据您的条件查询,这里是如何userUserRole表中检索。

def criteria = UserRole.createCriteria()
def result = criteria.list {
    projections {
        property("user") 
    }
    user {
        eq("username", "omar") //strict match
    }
}
return result;

它的作用是构建一个包含user.usernameas的列表omar。默认情况下,结果将是UserRole对象,但为了获取user对象,我们使用了投影。

更新:
您的 Domain 类似乎有点超出 grails 约定。为什么不使用这样的东西?

//User.groovy
class User {
    String username
    // ..
    static constraints = {
        username blank: false, nullable: false, maxSize: 10
    }
}

//UserRole.groovy
// Since you are using composite keys, we have to implement serializable. But is there a need for composite key in your situtation?
class UserRole implements Serializable {
    User user
    // ..
}

在你的服务类

// UserRoleService.groovy
def getUser(String username) {
    def criteria = UserRole.createCriteria()
    def result = criteria.get {
        projections {
            property("user") 
        }
        user {
            ilike("username", username)
        }
    }
    return result;
}

并从控制器调用服务方法,

// ExampleController.groovy
class ExampleController {
    def userRoleService

    def index() {}
    def someThing(String username) {
        def user = userRoleService.getUser(username)
        println user?.username
        // ..
    }
}
于 2013-09-04T06:02:39.233 回答