Grails一对多单向关联无需联接表是否可能?

huangapple go评论51阅读模式
英文:

Grails One-To-Many unidirectional without join table is it possible?

问题

Grails One-To-Many单向关系,不创建连接表,无法正常工作。我按照这里提到的文档尝试过。

当我按照文档创建一个领域类时,它会创建连接表。这是一个错误还是我的理解有误?以下是我使用的代码:

class Person {
    String bookName
    static hasMany = [addresses: Address]
    static mapping = {
        addresses column: 'person_address_id'
    }
}

class Address {
    String address
    static constraints = {
    }
}

生成的表EER模型如下:

Grails一对多单向关联无需联接表是否可能?

注意: 在application.yml文件中使用datasource.dbCreate为"create-drop"。

英文:

Grails One-To-Many unidirectional without creating join table is not working. I tried as per Documentation mentioned here.

When I tried creating an Domain class as per the documentation its creating the Join Table. Is this a bug or my understanding is wrong?. Here is the code which I used

class Person {
    String bookName
    static hasMany = [addresses: Address]
    static mapping = {
        addresses column: 'person_address_id'
    }
 }

 class Address {
    String address
    static constraints = {
    }
}

And the resulting table EER Model is

Grails一对多单向关联无需联接表是否可能?

NOTE: Using datasource.dbCreate as "create-drop" in application.yml file.

答案1

得分: 2

无连接表的单向映射仅在反转域类之间的关系时才可能:

class Person {}

class Address {
  static belongsTo = [ person: Person ]
}

通常我会选择这种方式,因为它具有比其他方式更好的性能和一致性。

唯一缺失的开箱即用功能是“给我所有属于某人的地址”这种情况,可以相对容易地建模:

class Person {

  List<Address> getAddresses(params = [:]) {
    Address.findAllByPerson(this, params)
  }

}
英文:

The unidirectional mapping w/o join table is only possible, if you invert the relation between your domain classes:

class Person {}

class Address {
  static belongsTo = [ person:Person ]
}

I usually go that way, as it has the better performance and consistency comparing to others.

The only thing which is missing out-of-box is give me all addresses for person-case, which can be modelled relatively easy:

class Person {

  List&lt;Address&gt; getAddresses( params = [:] ) {
    Address.findAllByPerson this, params
  }

}

huangapple
  • 本文由 发表于 2023年5月10日 18:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定