Gorm 查找查询

Gorm Find Query

事实 - 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

因为 Transaction 域中没有名为 accountId 的 属性。您的查询应该是-

def allTransactionsByAccountId = Transaction.findAllByAccount(Account.get(accountId))

而且我认为 transactionInstanceTotal: allTransactionsByAccountId.count() 在控制器中也是错误的。应该是

transactionInstanceTotal: Transaction.count()
TransactionController.groovy
def showTransactionsForAccount() {
    def accountId = params.selectedAccount
    def allTransactionsByAccountId  = Transaction.findAllByAccount(Account.get(accountId))
    render(view: "list", model: [transactionInstanceList: allTransactionsByAccountId , transactionInstanceTotal: allTransactionsByAccountId .size()])
}

这还不是优化方法或最佳实践,只是在进行中。