问题描述
在我的旧java代码中,我可以这样做,以便在hibernate查询中连接另一个表并根据它来过滤结果。
final Criteria brokerageCriteria = userCriteria.createCriteria(brokerage);
if(queryDto.getBrokerageID()!= null){
brokerageCriteria.add(Restrictions.eq(id,queryDto.getBrokerageID()));
$ b在这个例子中,它将过滤仅属于特定经纪商的用户(每个用户有一个经纪人)。
正如你所看到的,我可以通过调用criteria.createCriteria(String associationName)轻松地加入其他与休眠映射关联的表。 Grails中有类似的东西吗?我试图为我们的内部开发人员构建一个通用的JSON-to-criteria web api来搜索各种表格,页面通过数据等。
解决方案 div>考虑到场景,这里是域类和条件查询
//域用户
类用户{
//每个用户有一个经纪人
static hasOne = [经纪人:Brokerage ]
}
//域经纪
类经纪{
//可选
//静态belongsTo = [用户:用户]
}
$ b $ //标准查询
def user = User.createCriteria()。get {
brokerage {
idEq(queryDto.getBrokerageID())
}
}
或者您也可以使用非DSL方式
def user = User.createCriteria()。get {
eq('brokerage.id',queryDto.getBrokerageID())
在Grails中 criteria ,因为它使用DSL(域特定语言)。在上面的例子中,只有在经纪{} 的条件中提供关联,INNER JOIN在 User 和 Brokerage 基于 brokerageId 。
grails中的 Criteria 是您可以动态处理您的查询。例如,如果用户有一个经纪和一个按揭和许多 Lien s,并且您希望从系统获取用户,如果任何<$ c $在JSON中提供了c> brokerageId , mortgageId , lienId 。上面的标准可以有效地增强,因为
def user = User.createCriteria()。get {
if(queryDto .getBrokerageID()!= null){
brokerage {
idEq(queryDto.getBrokerageID())
}
} else if(queryDto.getMortgageID()!= null){
抵押{
idEq(queryDto.getMortgageID())
}
} else if(queryDto.getLienID()!= null){
liens {
idEq(queryDto.getLienID())
}
}
}
class User <$ b>用户 $ b //每个用户有一个经纪和一个抵押
static hasOne = [经纪:经纪,抵押:抵押]
static hasMany = [留置权:留置权] //许多留置权
}
In my old java code I could do this to "join" another table in a hibernate query and filter results based on it.
final Criteria brokerageCriteria = userCriteria.createCriteria("brokerage"); if (queryDto.getBrokerageID() != null) { brokerageCriteria.add(Restrictions.eq("id", queryDto.getBrokerageID())); }In this example it would filter users who are a member of a specific brokerage only (each user has one brokerage).
As you can see I can easily join other tables that are associated in the hibernate mappings by calling criteria.createCriteria(String associationName). Is there anything similar to this in grails? I am trying to build a general purpose JSON-to-criteria web api for our internal developers to search various tables, page through data, etc.
解决方案Considering the scenario, here is the domain classes and criteria query
//Domain User class User{ //each user has one brokerage static hasOne = [brokerage: Brokerage] } //Domain Brokerage class Brokerage{ //Optional //static belongsTo = [user: User] } //Criteria Query def user = User.createCriteria().get{ brokerage{ idEq(queryDto.getBrokerageID()) } }or You can also use the non-DSL way
def user = User.createCriteria().get{ eq('brokerage.id', queryDto.getBrokerageID()) }Joining associated tables in easier in case of criteria in grails because it uses DSLs(domain specific language). In the above example, only by providing the association in the criteria as brokerage{} an INNER JOIN is done on User and Brokerage based on brokerageId.
The beauty of Criteria in grails is that you can dynamically handle your query. For example, if a User has one Brokerage and one Mortgage and many Liens, and you want to get an User from the system if any of the brokerageId, mortgageId, lienId is provided in the JSON. The above criteria can be effectively enhanced as
def user = User.createCriteria().get{ if(queryDto.getBrokerageID() != null){ brokerage{ idEq(queryDto.getBrokerageID()) } } else if(queryDto.getMortgageID() != null){ mortgage{ idEq(queryDto.getMortgageID()) } } else if(queryDto.getLienID() != null){ liens{ idEq(queryDto.getLienID()) } } }User domain would look like
class User{ //each user has one brokerage and one mortgage static hasOne = [brokerage: Brokerage, mortgage: Mortgage] static hasMany = [liens: Lien] //many Liens }
这篇关于我如何添加一个标准到grails标准来限制基于过滤关联的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 16:36