gpt4 book ai didi

hibernate - GORM Hibernate查询

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

我有以下Grails域对象

class ProductType {
String name
static hasMany = [attributes: Attribute]
}

class Attribute {

Boolean mandatory = false
Integer seq

static belongsTo = [productType: ProductType]
}

我想获取所有 ProductType和它们的必需 Attribute。此外,我希望选择的 Attribute能够通过 seq属性被急切地加载和排序。我已经尝试过各种HQL和Criteria查询,但似乎无法弄清楚。

最佳答案

此查询将返回所有具有强制属性的产品类型,这些属性会被紧急加载:

def types = ProductType.executeQuery("""
select distinct type from ProductType type
left join fetch type.attributes attribute
where attribute.mandatory=true""")

这些属性位于映射集中,因此没有顺序,但是很容易收集和排序它们:
def sortedAttributes = types.collect { it.attributes }.flatten().sort { it.seq }

关于hibernate - GORM Hibernate查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1429848/

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