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

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

问题描述

我有一个域类使用一些临时属性foo。现在我想在此属性上使用listOrderByFoo,但是我收到错误无法解析属性:foo。
有没有办法在listOrderByProperty()或findByProperty()中使用临时属性?

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

Bar.findAllByFooIsNotNull()
Bar.listOrderByFoo()


解决方案

不幸的是,像Matt在他对你的问题的评论中说的那样,因为这些字段被标记为暂时的,所以它们不会被保留在数据库中,因此你无法查询它们。如果要通过transient属性查找或列出,则需要编写一个闭包来迭代一个已经设置的transient属性的对象列表。没有动态的GORM方法可以用来实现。

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

//找到foo = 3
bars.find {it.foo == 3}

//按照foo
条排序.sort {a,b - > a.equals(B)? 0:a.foo< b.foo? -1:1}


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()

解决方案

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方法中使用transient属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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