事实的 - JDK 1.6和Grails 2.0.0
我想在下面列出的Grails应用程序中编写一个groovy查询,以执行类似于下面的SQL的操作,
Select t.debtor, t.creditor From Transaction t, Account a where
a.account_id = ?
但是我收到错误
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
}
}
交易
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 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
答案 0 :(得分:1)
因为accountId
域中没有名为Transaction
的属性。您的查询应该是 -
def allTransactionsByAccountId = Transaction.findAllByAccount(Account.get(accountId))
我认为transactionInstanceTotal: allTransactionsByAccountId.count()
在控制器中也是错误的。应该是
transactionInstanceTotal: Transaction.count()
答案 1 :(得分:0)
TransactionController.groovy
def showTransactionsForAccount() {
def accountId = params.selectedAccount
def allTransactionsByAccountId = Transaction.findAllByAccount(Account.get(accountId))
render(view: "list", model: [transactionInstanceList: allTransactionsByAccountId , transactionInstanceTotal: allTransactionsByAccountId .size()])
}
这不是优化或最佳实践,只是正在进行中。