繁体   English   中英

grails gorm空字段

[英]grails gorm null fields

我有两个域类:

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-object中的一个)中都只有空(空)字段。 当我不设置mainAddress时,addresses-list中的address-object将正确设置所有字段。 数据库字段(到目前为止,我使用in-memory-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))

有谁知道,为什么我的映射不起作用?

谢谢您的帮助

罗兰

您的模型有很多问题,最严重的是person-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
    }
}

我认为这符合您的要求,但是它是一个简单的模型,因为“ Person和“ Address之间只有一个(一对多)关系

我现在找到了一个解决方案:

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的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM