addto*를 무시하고* Gorm/Grails 방법을 제거하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1461857

  •  13-09-2019
  •  | 
  •  

문제

Grails/Gorm이 제공 한 동적 방법 Addto*를 무시하려고했지만 작동하지 않는 것 같습니다.

코드는 다음과 같습니다.

class Match {
    static hasMany = [players: Player, matchPlayers: MatchPlayer]

    void addToPlayers(Player player) {
        if (players.add(player)) {
            MatchPlayer matchPlayer = new MatchPlayer(match: this, player: player)
            matchPlayers.add(matchPlayer)
        }
    }
}
ma = new Match().save()
ma.addToPlayers(player1)

문제는 AddToplayers를 호출 할 때 다음과 같은 예외를 얻었습니다.

java.lang.NullPointerException: Cannot invoke method add() on null object

기본적으로 '플레이어'컬렉션을 초기화 해야하는 것 같습니다.

글쎄, 그렇게하기 전에, 나는 Gorm 메커니즘에 대한 통찰력을 갖고 싶습니다.

1- Gorm 컬렉션의 기본 구현은 무엇입니까 (Java.util.set의 구현이지만 어느 것입니까?)

2- (AddToplayers 메소드를 재정의함으로써)해야 할 일입니까? (내 유일한 요구는 플레이어가 매치 인스턴스에서 추가/제거 될 때마다 객체 매치 플레이어를 생성/제거하는 것입니다). 그렇다면 왜 예외가 있습니까? 이것에 대한 더 나은 디자인이 있습니까?

고맙습니다.

도움이 되었습니까?

해결책

귀하의 코드는 표준 Grails 접근 방식과 유사합니다. org.codehaus.groovy.grails.pluginsdomainclassgrailsplugin.addrelationshipmanagementmethods ()의 코드를 참조하십시오. 초기 세트 구현은 컬렉션 유형을 지정하지 않으면 해시 세트 또는 SortedSet을 지정하는 경우 트리셋입니다.

static hasMany = [players: Player, matchPlayers: MatchPlayer]
SortedSet players
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top