grails gorm空字段
我有两个域类:grails gorm空字段,grails,gorm,Grails,Gorm,我有两个域类: class Person { String lastname String firstname String alias Date birthday String notes static belongsTo = [addressBook: AddressBook, mainAddress: Address] static hasMany = [tags: Tag, addresses: Address] sta
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
}
}
及
每个人都有多个地址,可以将一个地址定义为主地址
在我的控制器里,我做一个
params.max = Math.min(max ?: 10, 100)
respond Person.list(params)
将所有地址加载到所有人员。我收到的person对象包含一个包含所有地址和主地址的地址列表。但是用作主地址的地址在两个对象(列表中的一个对象和主地址对象)中只有空(null)字段。当我没有设置主地址时,地址列表中的地址对象会正确设置所有字段。数据库字段(到目前为止我使用内存中的数据库)似乎是正确的:
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 Main Address关系没有所有者,即每一方都属于另一方。如果可能的话,我会把你的模型简化为
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
}
}
班级人员{
//忽略非地址属性,因为它们与此问题无关
静态hasMany=[地址:地址]
地址getMainAddress(){
Address.createCriteria().get{
情商“人”,这个
eq‘isMain’,没错
}
}
静态瞬态=['mainAddress']
静态约束={
地址可为空:true,验证程序:{addresses->
//这可以防止一个人拥有多个主地址
地址?.count{it.isMain}我现在找到了一个解决方案:
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\u address,而main address通过地址表中的main\u address\u id进行映射-映射是必需的,因为主地址的字段没有填充。类Person belongsTo Address和Address belongsTo Person?你一定是在开玩笑你可能是说Person有一个地址和Address belongsTo Person
?谢谢你的回答,我也尝试过了,这很好用ing.我更喜欢我发布的解决方案,因为有了isMain标志,每次我必须找到主地址时,我都必须单独迭代地址列表。@罗兰不一定,我已经在Person
中添加了一个getMainAddress()
方法来检索主地址(如果存在的话)不需要迭代所有地址提供getter提供了一个很好的解决方案,这有利于我的解决方案,它不需要额外的连接表。它也更灵活,因为您只在真正需要的情况下查询主地址。非常感谢。
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 {
}