英文:
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模型如下:
注意: 在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
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<Address> getAddresses( params = [:] ) {
Address.findAllByPerson this, params
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论