Grails GORM使用hasMany映射同一类的两个属性
我有以下资料:Grails GORM使用hasMany映射同一类的两个属性,grails,mapping,gorm,Grails,Mapping,Gorm,我有以下资料: class Match{ Team localTeam Team visitingTeam } class Team{ static hasMany = [matches: Match] } class Team{ static hasMany = [matches: Match] static mappedBy = [matches: localTeam] } 这意味着: 加载插件管理器时出错:类[class myapp.Team
class Match{
Team localTeam
Team visitingTeam
}
class Team{
static hasMany = [matches: Match]
}
class Team{
static hasMany = [matches: Match]
static mappedBy = [matches: localTeam]
}
这意味着:
加载插件管理器时出错:类[class myapp.Team]中的属性[matches]是双向的一对多,其反面有两个可能的属性。命名关系另一端的一个属性[team],或使用“mappedBy”静态定义映射关系的属性。示例:静态mappedBy=[匹配项:'myprop']
因此,我使用“mappedBy”:
class Team{
static hasMany = [matches: Match]
static mappedBy = [matches: localTeam, matches: visitingTeam]
}
但是,通过这样做,当我从db获得一个团队时,matches Set只包含该团队是来访团队的比赛,这意味着它只将比赛映射到来访团队
如果我对以下内容进行编码:
class Match{
Team localTeam
Team visitingTeam
}
class Team{
static hasMany = [matches: Match]
}
class Team{
static hasMany = [matches: Match]
static mappedBy = [matches: localTeam]
}
它只映射本地团队的匹配项
是否有一种方法可以将这两项比赛(当团队是本地的,当团队是访客时)映射到团队?请先阅读关于GORM绩效问题的文章: 这可能就是你想要的:
class Team {
String name
String description
static constraints = {
name blank: false, nullable: false
description blank: true, nullable: true
}
static mapping = {
description type: 'text'
}
Set<Match> getHomeMatches() {
Match.findAllByHomeTeam(this).collect { it.homeTeam } as Set
}
Set<Match> getMatches() {
Match.findAllByTeam(this).collect { it.team } as Set
}
}
class Match {
Team homeTeam
Team team
static constraints = {
homeTeam nullable: false
team nullable: false
}
static mapping = {
id composite: ['homeTeam', 'team']
}
}
班级团队{
字符串名
字符串描述
静态约束={
名称空白:false,可空:false
说明空白:真,可空:真
}
静态映射={
描述类型:“文本”
}
设置getHomeMatches(){
Match.findAllByHomeTeam(this.collection{it.homeTeam}作为集合
}
设置getMatches(){
Match.findAllByTeam(this.collect{it.team}作为集合
}
}
班级比赛{
主队
团队
静态约束={
主队可为空:false
团队可为空:false
}
静态映射={
id组合:['homeTeam','team']
}
}
请先阅读关于GORM性能问题的文章:
这可能就是你想要的:
class Team {
String name
String description
static constraints = {
name blank: false, nullable: false
description blank: true, nullable: true
}
static mapping = {
description type: 'text'
}
Set<Match> getHomeMatches() {
Match.findAllByHomeTeam(this).collect { it.homeTeam } as Set
}
Set<Match> getMatches() {
Match.findAllByTeam(this).collect { it.team } as Set
}
}
class Match {
Team homeTeam
Team team
static constraints = {
homeTeam nullable: false
team nullable: false
}
static mapping = {
id composite: ['homeTeam', 'team']
}
}
班级团队{
字符串名
字符串描述
静态约束={
名称空白:false,可空:false
说明空白:真,可空:真
}
静态映射={
描述类型:“文本”
}
设置getHomeMatches(){
Match.findAllByHomeTeam(this.collection{it.homeTeam}作为集合
}
设置getMatches(){
Match.findAllByTeam(this.collect{it.team}作为集合
}
}
班级比赛{
主队
团队
静态约束={
主队可为空:false
团队可为空:false
}
静态映射={
id组合:['homeTeam','team']
}
}
试试这个
class Team {
static hasMany = [localTeamMatches: Match, visitingMatches: Match]
static mappedBy = [localTeamMatches: "localTeam", visitingMatches: "visitingTeam"]
}
试试这个
class Team {
static hasMany = [localTeamMatches: Match, visitingMatches: Match]
static mappedBy = [localTeamMatches: "localTeam", visitingMatches: "visitingTeam"]
}
这是一个非常有趣的方法,但不是我正在寻找的。这是一个非常有趣的方法,但不是我正在寻找的。主要问题是我只想维护一个集合“匹配”,而不是两个。主要问题是我只想维护一个集合“匹配”,没有解决办法吗?有解决办法吗?