English 中文(简体)
习俗婚姻 表. 领域
原标题:Criteria Many-to-Many With Custom Join Table Domain

鉴于以下领域:

class UserRole implements Serializable {
  User user
  Role role
}

class User { 
  Set<Role> getRoles() {
    UserRole.findAllByUser(this).collect { it.role } as Set
  } 
}

class Role { 
  Set<User> getUsers() {
    UserRole.findAllByRole(this).collect { it.user } as Set
}

I can t figure out how to build the criteria to find all the users with a given role. I tried the following:

def crit = User.createCriteria()
def results = crit.list {
  roles {
    eq( authority ,  ROLE_ADMIN )
  }
}

然而,它说,它无法在使用者中找到财产角色。 因此,我需要一个标准,因为用户将有更多的财产被搜寻到如此充满活力的寻找者为这种情况而赢得的拖车。

最佳回答

如果你的预期结果很小,那么这样做可能非常容易:

def c = UserRole.createCriteria()
def users = c.list {
    role {
        eq( authority ,  ROLE_ADMIN )
    }
    user {
        // additional user property constraints
    }
}.collect { it.user }

如果你期望取得大量成果,或需要把成果放在桌面上,那么我就不是肯定的。 我在那里 throw倒了,但我从未尝试过。 我不知道你是否能够使用<代码>预测值{财产(协会)},并且能够工作。

def c = UserRole.createCriteria()
def users = c.list {
    projections {
        property( user ) // never tried this, but worth a shot
    }
    role {
        eq( authority ,  ROLE_ADMIN )
    }
    user {
        // additional user property constraints
    }
}

我不认为你试图以你为榜样做些什么会奏效,因为你实际上并没有在你的<代码>上界定关系。 用户或 参考<代码>的类别 用户Role (i.e. with a hasMany).

问题回答

暂无回答




相关问题
grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

Error loading the grails gwt module xml

I ve installed the plugin from this article by Peter http://www.cacoethes.co.uk/blog/groovyandgrails/the-command-pattern-w.... While compile time its not able to find the module file which is present ...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

热门标签