grails GORM derived properties

Matt Westlake

I have a field in my grails domain object that I want to be a derived field from other tables. My domain looks like the following

class VotingTally{

   int votesSource1
   int votesSource2
   int totalVotes

}

static mapping = {
    totalVotes formula: (votesSource1.numOfVotes+ votesSource2.numOfVotes)
}

The error I get is

No such property: votesSource1 for class: org.grails.datastore.mapping.config.groovy.MappingConfigurationBuilder
Igor Artamonov

First of all, formula should be a string with SQL expression (you have Groovy expression). Like:

static mapping = {
    totalVotes formula: 'numOfVotes1 + numOfVotes2'
}

But in your case you want to calculate value from joined tables, and that's not possible. You just cannot add JOIN from here.

Seems that you have only one way - load it from the code:

static transients = ['totalVotes']

int getTotalVotes() {
    VotesSource.get(votesSource1).numOfVotes + VotesSource.get(votesSource2).numOfVotes 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related