在 findBy 或 listOrderBy 方法中使用瞬态属性 [英] Using transient property in findBy or listOrderBy methods

查看:16
本文介绍了在 findBy 或 listOrderBy 方法中使用瞬态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一些瞬态属性 foo 的域类.现在我想在这个属性上使用 listOrderByFoo 但我收到错误无法解析属性:foo".有没有办法在 listOrderByProperty() 或 findByProperty() 中使用瞬态属性?

I have a domain class using some transient property foo. Now I want to use listOrderByFoo on this property but I get the error "could not resolve property: foo". Is there any way to use transient properties in listOrderByProperty() or findByProperty() ?

class Bar {
 String name
 static transients = ['foo']
 def getFoo() {
   ...
 }
}

Bar.findAllByFooIsNotNull()
Bar.listOrderByFoo()

推荐答案

很遗憾没有.就像马特在他对您的问题的评论中所说的那样,由于这些字段被标记为瞬态,因此它们不会持久保存到数据库中,因此您无法查询它们.如果你想通过一个瞬态属性查找或列出,你需要编写一个闭包来迭代一个已经设置了瞬态属性的对象列表.没有动态 GORM 方法可用于执行此操作.

Unfortunately no. Like Matt said in his comment to your question, since those fields are marked as transient, they are not persisted to the database and thus there's no way for you to query them. If you want to find or list by a transient property, you'll need to write a closure to iterate over a list of objects with the transient property already set. There's no dynamic GORM method that you can use to do it.

def bars = [ new Bar(foo:1), new Bar(foo:2), new Bar(foo:4), new Bar(foo:3) ];

// Find bar with foo=3
bars.find { it.foo == 3 }

// Sort bars by foo
bars.sort { a,b -> a.equals(b)? 0: a.foo<b.foo? -1: 1 }

这篇关于在 findBy 或 listOrderBy 方法中使用瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆