Grails findAllBy() not working

1.5k views Asked by At

I have a table with columns like: id, externalId, firstname, lastname, email

My Domain:

class Employee {

int id
String externalId

static mapping = {
    table "Employee"

    id column: "Id"
    externalId column: "externalId"
    version false
   }    
}  

My Service:

class EmployeeService {

def getEmployee(externalId) {
    def employee = new Employee();

    employee.findAllByExternalId("1234");

    return employee
}

Error message in logs say:

No signature of method Employee.findAllByExternalId is applicable for argument types (java.lang.String_ values: [1234]

My goal is to load an instance of Employee where the externalId matches what I'm giving it. What am I doing wrong?

Thanks!

3

There are 3 answers

2
V H On

You do know there is a lot wrong with all of that

//Create an instance of Employee which currently holds nothing
def employee = new Employee();
//With this empty object of Employ now do findAllByExternalId
    employee.findAllByExternalId("1234");

You should be looking up

 //This will provide you with the entire employee 
 // domain class any it finds with that externalId. 
 // since it is declared as findAll 
 // the return will naturally be a list even though there may only be 1
 def employees = Employee?.findAllByExternalId("1234")

But that is all very long winded what are you trying to do verify it exists or return a list of all employees that have that id ? You refer it to it as getEmployee so I presume you are trying to find one and yet you are looking for an iteration by doing findAll

// If you wanted just to check it exists return a boolean like this
//This will return result as a boolean
boolean getEmployee(externalIdd) {
  return  Employee.where { externalId == externalIdd }.exists()
}

//if you wanted purely employee entire object bound to first records
Employee getEmployee(externalIdd) {
  return  Employee.where { externalId == externalIdd }?.find()
}

//if you wanted a list of entire employees like the findAll above
List<Employee> getEmployee(externalIdd) {
  return  Employee.where { externalId == externalIdd }?.findAll()
}

are some alternative methods and possible less intense on db depending on what you are trying achieve specifically the boolean vs your current method

Also I called externalIdd since when it comes .where sometimes having same variable names as what is called causes issues hence the alteration

E2A As per your comment if you only require id then an id usually is Long hence the strict definition of Long you may use def - def is more generic and will return anything. I have refined the where to only include property of id so if it finds record it will return id then ?:0L if nothing found return 0LFor long so return a zero Long you could replace that with ?:null or not declare it at all.

//if you only want to get the id as per comment
    Long getEmployeeId(String externalIdd) {
      return  (Employee.where{externalId == externalIdd}.property('id')?\
      .find()?:0L)
      // Another way close to your findBy but manually declared
      // and only returning id field.
      // return Employee.find{externalId==externalIdd}?.id
    }
0
Bryan G Campbell On

You should call findAllBy_() on the Class, not the instance. Also, since you are returning only one Employee and not a list, you should use the findBy_(). Given that, your service method should look like this:

def getEmployee(String externalId){
    return Employee.findByExternalId(exteranlId)
}
0
quindimildev On

You can not have a property ended by Id.

myInstance.propertyId means get the property "Property" and its Id.

And I guess there is a problem during the dynamic finders generation cause this-