2014-09-03 59 views
0

我有兩個領域類:Grails的格姆空字段

class Person { 

    String lastname 
    String firstname 
    String alias 
    Date birthday 
    String notes 
    static belongsTo = [addressBook: AddressBook, mainAddress: Address] 
    static hasMany = [tags: Tag, addresses: Address] 

    static constraints = { 
     mainAddress nullable: true 
     addresses nullable: true 
     alias nullable: true 
     birthday: nullable: true 
     tags nullable: true 
     notes nullable: true 
    } 
} 

class Address { 

    AddressType addressType 

    static belongsTo = [person: Person] 
    String company 
    String street 
    String zipCode 
    String city 
    String eMail 
    String phone 
    String mobile 
    String website 

    static constraints = { 
     person nullable: true 
     company nullable: true 
     website nullable: true 
    } 
} 

它的目的,每個人都有多個地址,可以定義一個地址作爲主地址。

在我的控制我做一個

params.max = Math.min(max ?: 10, 100) 
respond Person.list(params) 

加載所有地址的所有的人。我收到的個人對象包含一個包含所有地址和mainAddress的地址列表。但是,用作主地址的地址在兩個對象(列表中的一個和mainAddress對象中)中只有空(空)字段。當我不設置mainAddress時,地址列表中的地址對象帶有正確設置的所有字段。數據庫字段(我用的是內存-DB迄今爲止)似乎是正確的:

create table address (id bigint generated by default as identity, version bigint not null, address_type varchar(255) not null, city varchar(255) not null, company varchar(255), e_mail varchar(255) not null, mobile varchar(255) not null, person_id bigint, phone varchar(255) not null, street varchar(255) not null, website varchar(255), zip_code varchar(255) not null, primary key (id)) 
create table person (id bigint generated by default as identity, version bigint not null, address_book_id bigint not null, alias varchar(255), birthday timestamp not null, firstname varchar(255) not null, lastname varchar(255) not null, main_address_id bigint, notes varchar(255), primary key (id)) 

有沒有人一個想法,爲什麼我的映射不工作?

感謝您的幫助提前

羅蘭

+0

類人belongsTo地址和地址belongsTo人?你一定是在開玩笑 – injecteer 2014-09-03 13:40:44

+0

你可能是指「人有一個地址」和「地址屬於人」? – injecteer 2014-09-03 13:41:57

回答

1

有許多事情錯了你的模型,其中最嚴重的是,這人-mainAddress關係沒有所有者,即每邊belongsTo其他。如果可能的話,我將簡化你的模型

class Person { 
    // non-address properties omitted, because they're not relevant to this question 
    static hasMany = [addresses: Address] 

    Address getMainAddress() { 
     Address.createCriteria().get { 
      eq 'person', this 
      eq 'isMain', true 
     } 
    } 

    static transients = ['mainAddress'] 

    static constraints = { 

     addresses nullable: true, validator: { addresses -> 

      // this prevents a person from having more than one main address 
      addresses?.count { it.isMain } <= 1 
     } 
    } 
} 

class Address { 
    // non-person properties omitted, because they're not relevant to this question  
    boolean isMain = false 

    static belongsTo = [person: Person] 

    static constraints = { 
     // is it really OK for an address not to be associated with a person? 
     person nullable: true 
    } 
} 

我覺得這符合你的要求,但它是一個簡單的模型,因爲只有PersonAddress

+0

謝謝你的回答,我已經試過了,哪一個完全正常。我更喜歡我發佈的解決方案,因爲使用isMain-flag時,我必須在每次必須找到mainAddress時單獨遍歷地址列表。 – Roland 2014-09-04 13:26:48

+0

@Roland不一定,我在'Person'中增加了一個'getMainAddress()'方法,它將檢索主地址(如果存在的話)而不重複所有地址 – 2014-09-08 23:24:49

+0

提供getter提供了一個很好的解決方案,到我的解決方案不需要額外的連接表。它也更加靈活,因爲只有在真正需要的情況下才能查詢mainAddress。非常感謝。 – Roland 2014-09-10 05:43:39

1

之間的單(一個一對多)的關係現在我已經找到了解決辦法:

class Person { 

    Address mainAddress 
    static hasMany = [addresses: Address] 

    static constraints = { 
     addresses nullable: true 
     mainAddress nullable: true, unique: true 
    } 

    static mapping = { 
     addresses joinTable: [name: 'person_address',key: 'person_id', column: 'address_id'] 
     mainAddress lazy: false 
    } 
} 

class Address { 
} 

這樣的地址映射在加入表person_address,而mainAddress通過在地址表main_address_id映射。 「mainAddress:lazy」映射是必需的,否則mainAddress的字段不會被填充。