GORM mappedBy and mapping difference

你。 提交于 2019-12-12 09:59:08

问题


In the GORM what is the difference between mappedBy and mapping?

static mapping = {
...
}

static mappedBy = {
...
}

回答1:


mapping
mapping simply tells GORM to explicitly map one or more Domain properties to a specific database column.

class Person {
   String firstName

   static mapping = {
      table 'people'
      id column: 'person_id'
      firstName column: 'First_Name'
   }
}

in this case for instance I am instructing GORM to map the id attribute to the column person_id of the people table and the firstName property to the First_Name column of the same table.

mappedBy
mappedBy instead let you control unidirectionality or bidirectionality of your classes associations. From Grails documentation:

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

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

class Route {
    Airport departureAirport
    Airport destinationAirport
}

Airport defines two bidirectional one-to-many associations. If you don't specify mappedBy you would get an error because GORM cannot infer which of the two properties on the other end of the association (either departureAirport or destinationAirport) each one-to-many should be associated with.

In other words it helps you remove the ambiguity that comes from bidirectional associations.



来源:https://stackoverflow.com/questions/29921242/gorm-mappedby-and-mapping-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!