{"id":17109,"date":"2015-02-06T15:07:44","date_gmt":"2015-02-06T09:37:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17109"},"modified":"2015-07-24T16:59:56","modified_gmt":"2015-07-24T11:29:56","slug":"significance-of-mappedby-in-grails-domain","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/significance-of-mappedby-in-grails-domain\/","title":{"rendered":"Significance of \u201cmappedBy\u201d in Grails Domain"},"content":{"rendered":"<p>&#8220;mappedBy&#8221; is a static map which is used to determine and change the way two associated domain classes interact with each other.<\/p>\n<p>Let&#8217;s start with the role of &#8220;mappedBy&#8221;\u00a0in one-to-many relationships.<\/p>\n<p>Consider the following Example:-<\/p>\n<p>[code]class Team {<br \/>\n\u2003\u2003\u2003\u2003static hasMany = [members: TeamMember]<br \/>\n}[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p>[code]class TeamMember {<br \/>\n\u2003\u2003\u2003\u2003Team memberOf<br \/>\n\u2003\u2003\u2003\u2003Team captainOf<br \/>\n}[\/code]<\/p>\n<p>The Team domain contains a one-to-many association (members) with TeamMember domain which has multiple properties (memberOf, captainOf) of Team type. This makes the association bi-directional.<\/p>\n<p>Now, <a title=\"Grails App Development \" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails\u00a0won&#8217;t know which<\/a> of the properties (memberOf or captainOf) of TeamMember domain it needs to involve while creating this bi-directional one-to-many association and it will give the following error:<\/p>\n<pre>\"Property [members] in class [class Team] is a bidirectional one-to-many \r\nwith two possible properties on the inverse side. Either name one of the properties on \r\nother side of the relationship [team] or use the 'mappedBy' static to define the property \r\nthat the relationship is mapped with. Example: static mappedBy = [members:'myprop']\"<\/pre>\n<p>To resolve this, we declare &#8220;mappedBy&#8221; on the \u201cone\u201d side of this association:<\/p>\n<p>[code]class Team {<br \/>\n\u2003\u2003\u2003\u2003static hasMany = [members: TeamMember]<br \/>\n\u2003\u2003\u2003\u2003static mappedBy = [members: &#8216;memberOf&#8217;]<br \/>\n}[\/code]<\/p>\n<p>This way Grails would know that <strong>[members]<\/strong> of <strong>Team<\/strong> has a bi-directional one-to-many association with <strong>[memberOf]<\/strong> of <strong>TeamMember<\/strong> and it will infer that <strong>Team<\/strong> is associated with <strong>TeamMember<\/strong> in a one-to-one relationship using the property <strong>[captainOf]<\/strong>.<\/p>\n<hr \/>\n<p>On that note, lets consider the case of many-to-many relationships.<\/p>\n<p>Here&#8217;s an example :-<\/p>\n<p>[code]class User {<br \/>\n\u2003\u2003\u2003\u2003static hasMany = [writtenPosts: Article, subscribedPosts: Article]<br \/>\n}[\/code]<\/p>\n<p>[code]class Article {<br \/>\n\u2003\u2003\u2003\u2003User writer<br \/>\n\u2003\u2003\u2003\u2003static hasMany = [subscribers: User]<br \/>\n\u2003\u2003\u2003\u2003static belongsTo = [User]<br \/>\n}[\/code]<\/p>\n<p>These domains are associated with each other in 2 ways:<\/p>\n<ol>\n<li><strong>[writtenPosts]<\/strong> of <strong>User<\/strong> is associatemany-to-one relationship.d with <strong>[writer]<\/strong> of <strong>Article<\/strong> in a bi-directional<\/li>\n<li><strong>[subscribedPosts]<\/strong> of <strong>User<\/strong> is associated with <strong>[subscribers]<\/strong> of <strong>Article<\/strong> in a many-to-many relationship.<\/li>\n<\/ol>\n<p>With this code, Grails will create 4 tables for these 2 classes:<\/p>\n<pre>1. user(id, version, name)\r\n2. article(id, version, description, writer_id)\r\n3. user_subscribed_posts(user_id, article_id)\r\n4. user_written_posts(user_id, article_id)\r\n<\/pre>\n<p>The 4th table is redundant as the same information would be stored in <strong>[writer_id]<\/strong> column of <strong>article<\/strong> table. To prevent this, we declare \u201cmappedBy\u201d in the <strong>User<\/strong> domain specifying:<\/p>\n<ol>\n<li><strong>[writtenPosts]<\/strong> of <strong>User<\/strong> is associated with <strong>[writer]<\/strong> of <strong>[Article]<\/strong>.<\/li>\n<li><strong>[subscribedPosts]<\/strong> of <strong>User<\/strong> is associated with <strong>[subscribers]<\/strong> of <strong>Article<\/strong>.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>[code]class User {<br \/>\n\u2003\u2003\u2003\u2003static hasMany = [writtenPosts: Article, subscribedPosts: Article]<br \/>\n\u2003\u2003\u2003\u2003static mappedBy = [writtenPosts: &#8216;writer&#8217;, subscribedPosts: &#8216;subscribers&#8217;]<br \/>\n}[\/code]<\/p>\n<p>Now, when this code runs only 3 tables are created:<\/p>\n<pre>1. user(id, version, name)\r\n2. article(id, version, description, writer_id)\r\n3. user_subscribed_posts(user_id, article_id)\r\n<\/pre>\n<p>By using &#8220;mappedBy&#8221; we can direct Grails towards the correct relationship or change the inferred relationship between multiple domains to suite our needs.<\/p>\n<p>I came by this topic while perusing through <a title=\"Grails Wikipedia\" href=\"https:\/\/en.wikipedia.org\/wiki\/Grails_(framework)\">Grails\u00a0wiki<\/a> and thought of sharing this with everyone.<\/p>\n<p>Hope you enjoyed reading this blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;mappedBy&#8221; is a static map which is used to determine and change the way two associated domain classes interact with each other. Let&#8217;s start with the role of &#8220;mappedBy&#8221;\u00a0in one-to-many relationships. Consider the following Example:- [code]class Team { \u2003\u2003\u2003\u2003static hasMany = [members: TeamMember] }[\/code] &nbsp; [code]class TeamMember { \u2003\u2003\u2003\u2003Team memberOf \u2003\u2003\u2003\u2003Team captainOf }[\/code] The Team [&hellip;]<\/p>\n","protected":false},"author":159,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":21},"categories":[7],"tags":[4840,1630,1627,1629],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17109"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/159"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17109"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17109\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}