0

I'm trying to get the list of a specific rental from the current user.

Code in controller:

def index() { 
if (isLoggedIn()) {
    String username = getPrincipal().username

    def accountInstance = Account.findByUsername(username)
    def rentalInstanceList = Rental.findAll("from Rental as r where r.account_id=:accountid", [accountid: accountInstance.id])
    }
}

account_id is a foreign key.

After running I get the error:

could not resolve property: account_id of: ers.Rental

What am I doing wrong?

newbie
  • 119
  • 15

2 Answers2

1

Generally, in HQL you have to use the field names as defined in your domain classes. So, your query should look like:

def list = Rental.findAll("from Rental where accountId=:accountid", [accountid: accountInstance.id])

or

def list = Rental.findAllByAccount accountInstance

or even

def list = Rental.findAllByAccount getPrincipal()

if the return type of getPrincipal() has the id field.

injecteer
  • 20,038
  • 4
  • 45
  • 89
-1

findAll is not limited to instances of the calling class so I use executeQuery instead. https://stackoverflow.com/a/8916483/5011228

Community
  • 1
  • 1
newbie
  • 119
  • 15