Using the transient property in findBy or listOrderBy methods

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

class Bar { String name static transients = ['foo'] def getFoo() { ... } } Bar.findAllByFooIsNotNull() Bar.listOrderByFoo() 
+4
source share
1 answer

Unfortunately not. As Matt said in a comment on your question, since these fields are marked as temporary, they are not stored in the database, and therefore you cannot request them. If you want to find or list a transient property, you need to write a closure to repeat the list of objects with the transient property already set. There is no dynamic GORM method that you can use to do this.

 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 } 
+10
source

Source: https://habr.com/ru/post/1379804/


All Articles