(Quick Reference)

mappedBy

Purpose

The mappedBy static property adds the ability to change how a bidirectional association is linked

Examples

class Airport {

static mappedBy = [outgoingFlights: 'departureAirport', incomingFlights: 'destinationAirport']

static hasMany = [outgoingFlights: Route, incomingFlights: Route] }

class Route {
    Airport departureAirport
    Airport destinationAirport
}

In this example the Airport class defines two bidirectional one-to-many associations. Without defining mappedBy this is impossible as GORM cannot differentiate which of the two properties on the other end of the association (either departureAirport or destinationAirport in the Route class) each one-to-many should be associated with.

The solution is to define mappedBy which tells the Airport class how each association relates to the other side.