Java 如何显示基于外键groovy grails的列表
我有两个表:Java 如何显示基于外键groovy grails的列表,java,grails,groovy,Java,Grails,Groovy,我有两个表:地区和地区。因此,region\u id是表District中的外键(一个区域有一个或多个区域)。因此,当我在我的列表中选择地区时,我只想显示与特定地区相关的地区。 “我的正确代码”显示所有地区,独立于地区: def list = { params.max = Math.min(params.max? params.int('max') : 20, 100) [districtInstanceList : District.list(params),
地区
和地区
。因此,region\u id
是表District
中的外键(一个区域
有一个或多个区域
)。因此,当我在我的列表中选择地区
时,我只想显示与特定地区
相关的地区。
“我的正确代码”显示所有地区
,独立于地区
:
def list = {
params.max = Math.min(params.max? params.int('max') : 20, 100)
[districtInstanceList : District.list(params),
districtInstanceTotal: District.count()]
}
是否有人知道如何仅基于外键约束显示?我知道我可以在列表
闭包中编写SQL
查询,但我想grails可能有办法做到这一点。
我的数据库是MySQL
,而grails
版本是2.0.1
。
我的地区
域是:
class District {
def scaffold = true
String name
String description
String logo
String homepage
// defines the 1:n constrain with the Region table
static belongsTo = [region : Region]
// defines the 1: constraint with the Stream table
static hasMany = [streams : Stream]
static constraints ={
name(blank:false, minSize:6, maxSize:30)
description(blank: false, maxSize:100)
}
public String toString(){
name
}
}
您可以使用GORM:
def list = {
params.max = Math.min(params.max? params.int('max') : 20, 100)
Region region = Region.get(params.id) // or what parameter you're using
List districts = District.findAllByRegion(region)
[districtInstanceList : districts,
districtInstanceTotal: District.count()]
}
您可以在此处阅读有关Grails GORM的内容:您可以使用GORM:
def list = {
params.max = Math.min(params.max? params.int('max') : 20, 100)
Region region = Region.get(params.id) // or what parameter you're using
List districts = District.findAllByRegion(region)
[districtInstanceList : districts,
districtInstanceTotal: District.count()]
}
您可以在这里阅读Grails GORM:我得到:错误500:执行控制器[ctv.DistrictController]的操作[list]导致异常:groovy.lang.MissingMethodException:没有方法签名:ctv.District.findAllByRegion()适用于参数类型:()值:[]可能的解决方案:findAllByRegion(java.util.list)Servlet:grails
有什么办法可以解决这个问题吗region\u id
也是District
中的外键,感谢您的第一个想法。我为地区加入了我的域名
请检查通过的地区是否为空,你是对的。区域为空。另外,您能告诉我如何将该区域的id
传递给params
?我得到:错误500:执行控制器[ctv.DistrictController]的操作[list]导致异常:groovy.lang.MissingMethodException:没有方法签名:ctv.District.findAllByRegion()适用于参数类型:()值:[]可能的解决方案:findAllByRegion(java.util.List)Servlet:grails
知道如何解决这个问题吗region\u id
也是District
中的外键,感谢您的第一个想法。我为地区加入了我的域名
请检查通过的地区是否为空,你是对的。区域为空。另外,您能告诉我如何将区域的id
传递给params
?