Grails, how to find by record with your foreign key

I have two domains that are part of a one-to-many relationship ship. I was wondering how can I request a child for FC parents? below is the psuedo code for parent / child

Parent:

class AlumProfile { String firstName String lastName static hasMany = [alumLanguage : AlumLanguage] static mapping = { cache true id generator: 'assigned' columns { firstName type:'text' lastName type:'text' } // } static constraints = { firstName (nullable:true) lastName (nullable:true) } } 

Child:

  class AlumLanguage { String name String level static belongsTo = [alumProfile:AlumProfile] static mapping = { cache true columns { name type:'text' level type:'text' } } static constraints = { name(nullable:true) level(nullable:true) } } 

Although I clearly do not create FK, Grails itself takes care of creating a MySQL database. But, when I want to ask the child FK, like this:

  if(AlumLanguage.findByNameAndAlumProfileId(language.'language'.toString(), 'jIi-hRi4cI')==null){ //do something } 

I get an error: No properties found for name [alumProfileId] for class [class mgr.AlumLanguage]

Any suggestions on how to do this?

thanks jason

+6
source share
1 answer

Try using the criteria :

 def c = AlumLanguage.createCriteria() def languages = c.get { eq('name', 'whatever-language') alumProfile { eq('id', 'jIi-hRi4cI') } } 
+7
source

Source: https://habr.com/ru/post/894398/


All Articles