【发布时间】:2014-09-03 12:59:08
【问题描述】:
我有两个域类:
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 对象)中都只有空(null)字段。当我不设置 mainAddress 时,地址列表中的地址对象带有正确设置的所有字段。数据库字段(到目前为止我使用的是 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 hasOne Address和Address belongsTo Person?
标签: grails grails-orm