This questions concerns the use of the formula field in a static mapping block in Grails.
I am trying to implement a very simple domain class that uses a formula mapping (derived properties), but I keep getting a “No property found for name [uptime]“, where uptime is a derived property in the Grails domain class. The code is as follows (simplified):
class Derive {
Integer up
Integer down
static mapping = {
uptime formula : "UP/(DOWN+UP)"
}
}
class DeriveTests extends GroovyTestCase {
void testDerivedProp() {
new Derive(up:10, down:5).save()
new Derive(up:5, down:5).save()
assertEquals Derive.all.size(),2
assertEquals 2,Derive.findAllByUptimeGreaterThan(0.1).size() //fails here
assertEquals 2,Derive.findAllByUptimeGreaterThan(10/(10+5)).size()
}
}
Running the test gives me an error at the second assertEquals: org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [uptime] for class [class Derive]. I cannot see what I am doing wrong here, having consulted both Grails In Action and the reference docs several times.
Any clues as to what I am doing wrong? The backing db is a HSQLDB running in memory with default settings (created by grails create-app).
edit: I am a bit unsure whether I should add a property field for the formula or not. If I do add a property field ‘Double uptime’, the assert still fails, but this time because uptime is 0. Viewing the object in a debugger show that uptime is null. Still, the sql output shows me something that looks right: hibernate.SQL select this_.id as id6_0_, this_.version as version6_0_, this_.down as down6_0_, this_.up as up6_0_, this_.UP*100/(this_.DOWN+this_.UP) as formula0_0_ from derive this_
No new answers in a while, so I am posting a sum-up:
The last bit, manually refreshing, seems very unelegant, but I have not found a way around it.