Grails,如何通过外键找到记录 [英] Grails, how to find by a record by its foreign key
问题描述
我有两个域是一对多关系的一部分。我想知道如何查询孩子的父母FK? bellow是父母/子女的伪代码
父母:
class AlumProfile {
String firstName
String lastName $ b $ static static hasMany = [alumLanguage:AlumLanguage]
static mapping = {
cache true
id生成器:'assigned'
列{
名字类型:'text'
姓氏类型:'text'
}
//
}
静态约束= {
firstName(可空值:true)
lastName(可空值:true)
}
}
小孩:
class AlumLanguage {
String name
String level
$ b static belongsTo = [alumProfile:AlumProfile]
static mapping = {
缓存真正
列{
名称类型:'text'
等级类型:'text'
}
}
static constraints = {
名称(可空):
等级(n但是我没有明确地创建它们,但是我没有明确地创建它们。 FK,grails负责创建它自己的MySQL数据库。但是,当我想要这样的FK查询孩子: if(AlumLanguage.findByNameAndAlumProfileId(language.'language' .toString(),'jIi-hRi4cI')== null){
//做某事
}
我得到一个错误:找不到名称为[alumProfileId]的类[class mgr.AlumLanguage]
有关如何完成的任何建议这个?
感谢
jason
解决方案 条件:
def c = AlumLanguage.createCriteria()
def languages = c.get {
eq('name','whatever-language' )
alumProfile {
eq('id','jIi-hRi4cI')
}
}
I have two domains that are a part of a one-to-many relations ship. I was wondering how i can query the child for the parents FK? bellow 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 do not explicitly create the FK, grails takes care of creating it the MySQL DB on its own. But, when i want to query the child by the FK like this:
if(AlumLanguage.findByNameAndAlumProfileId(language.'language'.toString(), 'jIi-hRi4cI')==null){
//do something
}
I get an error: No property found for name [alumProfileId] for class [class mgr.AlumLanguage]
Any suggestions on how to accomplish this?
thanks
jason
解决方案 Try using a criteria:
def c = AlumLanguage.createCriteria()
def languages = c.get {
eq('name', 'whatever-language')
alumProfile {
eq('id', 'jIi-hRi4cI')
}
}
这篇关于Grails,如何通过外键找到记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!