Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

hibernate - Using transient property in findBy or listOrderBy methods

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

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 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...