Reputation: 709
Facts - JDK 1.6 and Grails 2.0.0
I would like to write a groovy query in the Grails application as listed below to do something similar to the SQL below,
Select t.debtor, t.creditor From Transaction t, Account a where
a.account_id = ?
However I am getting error
No property found for name [accountId] for class [class com.Transaction]
Account.groovy
package com
class Account {
String name
BigDecimal balance = 200
String emailAddress
static hasMany = [transactions: Transaction]
static constraints = {
name()
balance()
emailAddress()
}
String toString() {
return name + " " + balance
}
}
Transaction
package com
class Transaction {
String debtor
String creditor
BigDecimal amount
Date transactionDate
static belongsTo = [account: Account]
static constraints = {
}
String toString() {
return "debtor : " + debtor + " creditor " + creditor + " £" + amount + " - transaction Date : " + transactionDate
}
}
TransactionController.groovy
def showTransactionsForAccount() {
def accountId = params.selectedAccount
def allTransactionsByAccountId = Transaction.findAllByAccountId(accountId)
redirect(action: "list", model: [transactionInstance: allTransactionsByAccountId,
transactionInstanceTotal: allTransactionsByAccountId.count()])
}
Error Stack
Error 500: Internal Server Error
URI
/payment-app/transaction/showTransactionsForAccount
Class
org.codehaus.groovy.grails.exceptions.InvalidPropertyException
Message
No property found for name [accountId] for class [class com.Transaction]
Around line 20 of grails-app\controllers\com\TransactionController.groovy
17:
18: def showTransactionsForAccount() {
19: def accountId = params.selectedAccount
20: def allTransactionsByAccountId = Transaction.findAllByAccountId(accountId)
21:
22: redirect(action: "list", model: [transactionInstance: allTransactionsByAccountId,
23: transactionInstanceTotal: allTransactionsByAccountId.count()])
Trace
Line | Method
->> 105 | doCall in org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 20 | showTransactionsForAccount in TransactionController.groovy
| 895 | runTask . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run in ''
^ 662 | run . . . . . . . . . . . in java.lang.Thread
Upvotes: 0
Views: 194
Reputation: 709
TransactionController.groovy
def showTransactionsForAccount() {
def accountId = params.selectedAccount
def allTransactionsByAccountId = Transaction.findAllByAccount(Account.get(accountId))
render(view: "list", model: [transactionInstanceList: allTransactionsByAccountId , transactionInstanceTotal: allTransactionsByAccountId .size()])
}
This is no means optimized or a best practices yet, just work in progress.
Upvotes: 0
Reputation: 7619
Because there is no property named accountId
in Transaction
domain. Your query should be-
def allTransactionsByAccountId = Transaction.findAllByAccount(Account.get(accountId))
and I think transactionInstanceTotal: allTransactionsByAccountId.count()
is also wrong in the controller. Should be
transactionInstanceTotal: Transaction.count()
Upvotes: 1