kofhearts
kofhearts

Reputation: 3774

understanding mappedBy in grails

From the grails documentation, I am trying to understand the following example.

enter image description here

The tables that are created from these two domain definitions are as follows:

enter image description here

What i don't understand here is what is the purpose of mappedBy which is specified in the Airport domain? Thanks for the help!

Upvotes: 0

Views: 164

Answers (1)

stenix
stenix

Reputation: 3106

The mappedBy is used to know which relation (foreign key column) on the other side that should be used for the hasMany relation. It is only necessary if it is not obvious. In the example above, the flights relation should use departureAirports and mappedBy is necessary since destinationAirport is also a relation to the Airport class/table. If you omit mappedBy in this case, it will not be clear which relation to use and you may end up with a relation that is mapped by the wrong foreign key column. You could be lucky that it chooses the correct one but it is not deterministic and it may lead to unexpected behaviour.

The tables generated by the code will look the same without it though. It is the application usage/mapping of the tables that will differ.

Upvotes: 1

Related Questions