GORM映射了和映射的区别 [英] GORM mappedBy and mapping difference

查看:277
本文介绍了GORM映射了和映射的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GORM中, mappedBy 映射


$之间有什么区别b $ b

 静态映射= {
...
}

static mappedBy = {
...


解决方案

strong>>
mapping 只是简单地告诉GORM将一个或多个Domain属性显式映射到特定的数据库列。

  class Person {
字符串名字

静态映射= {
表格'people'
id列:'person_id'
firstName列:'First_Name'
}
}

在这种情况下,例如我正在指示GORM将id属性映射到 person_id > people 表和 firstName 属性添加到同一个表的 First_Name 列中。 / p>

mappedBy

mappedBy 您可以控制单向性或双向性你的班级协会。从 Grails文档

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

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

class Route {
机场出发机场
机场destinationAirport
}

机场定义了两个双向一对多关联。如果你没有指定 mappedBy ,你会得到一个错误,因为GORM无法推断关联另一端的两个属性中的哪一个( departureAirport destinationAirport )每个一对多都应该关联。





换句话说,它可以帮助您消除来自双向关联的歧义。


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

static mapping = {
...
}

static mappedBy = {
...
}

解决方案

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.

这篇关于GORM映射了和映射的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆