Search code examples
grailsgrails-ormone-to-one

grails findByDomain returns null


A domain Staff has a User.

class Person {
      User user
}

class Staff extends Person {
      //other properties
}

class User {
      String username
      String password
}

I know a user logged in, now I want to find the Staff by the same logged in User. No relation is maintained from User's side.

The code I am implementing is :

def createdBy = User.get(springSecurityService.principal.id)
log.info("User id : "+createdBy.id) // it works
def staff = Staff.findByUser(createdBy) //it returns null

Is this not applicable in GORM or I'm missing something?

grails findBy documentation has nothing to tell about findByDomain().


Solution

  • The question is CLOSED as the error was while inserting a Staff with a User which was not heppening in proper way.(poor grails didn't notify me.)

    Above code works perfectly.

    def createdBy = User.get(springSecurityService.principal.id)
    def staff     = Staff.findByUser(createdBy) 
    

    But, meanwhile implemented another way of finding Staff in criteria way :

        def createdBy = User.get(springSecurityService.principal.id)
        def staff     = staffCriteria.get{
            user{
                idEq(createdBy.id)
            }
        }