在另一个实体grails GORM中按id查找
我在grails映射中有2个实体 产品和资产 我需要获取产品id为x、资产类型为y的资产 我试过了在另一个实体grails GORM中按id查找,grails,gorm,Grails,Gorm,我在grails映射中有2个实体 产品和资产 我需要获取产品id为x、资产类型为y的资产 我试过了 Asset.findByProductIdAndAssetType(productId, assetType) 但是不起作用 唯一的解决方案是 按id加载产品并按加载的产品查找 def product = Product.findById(productId) Asset.findByProductAndAssetType(product, assetType) 是否有任何方法仅使用pro
Asset.findByProductIdAndAssetType(productId, assetType)
但是不起作用
唯一的解决方案是
按id加载产品并按加载的产品查找
def product = Product.findById(productId)
Asset.findByProductAndAssetType(product, assetType)
是否有任何方法仅使用productId加载资产?使用
load
方法通过一个数据库调用完成此操作:
Asset.findByProductAndAssetType(Product.load(productId), assetType)
load()
与get()
类似,它通过其id检索单个实例,但load()
仅在访问id以外的任何持久性属性后才延迟检索实例。get()
立即进行数据库查询并返回实例,如果未找到,则返回null
,但是load()
总是返回一个非空代理,当它进行数据库查询时,如果找到,它将从检索到的数据中填充其属性,如果没有,则抛出异常
但是在上面的解决方案中,没有对
Product
实例的数据库调用,因为检索Asset
所需的只是用作外键的id。Asset.where{Product.id==productId&&assetType==aType}?.find()
或Asset.find{Product.id==productId&&assetType==aType}