gpt4 book ai didi

grails - 编写条件以模仿联接查询

转载 作者:行者123 更新时间:2023-12-02 15:28:14 25 4
gpt4 key购买 nike

我正在使用grails 2.2.4,并希望编写可能生成SQL的条件,例如:

select r.value from group g, rating r where r.groupId = g.id and g.name = ?

提供以下域结构:
class Group {
String name
}

class Rating {
Long groupId
int value
}

我该如何使用grails标准来编写此代码?

我在写这样的东西:
def result = Rating.withCriteria {
projections {
property("value")
}
eq "groupId", new DetachedCriteria(Group).build {
projections {
property("id")
}
eq("name", "Group A")
}
}

但是从这开始,休眠产生了子查询。有没有猜到?

最佳答案

您可能应该将Domain结构更改为面向对象。为此使用关联:[http://grails.org/doc/2.2.4/guide/GORM.html#gormAssociation]

您的代码将如下所示:

class Group {
String name
}

class Rating {
Group group
int value
}

您的条件查询可以类似于以下内容:
Rating.withCriteria {
createAlias('group', '_group')
eq('_group.name', 'Group A')
projections {
property('value')
}
}

关于grails - 编写条件以模仿联接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com