gpt4 book ai didi

grails - Grails GORM一对一关系问题:列:(应使用insert =“false” update =“false”进行映射)

转载 作者:行者123 更新时间:2023-12-02 13:51:40 28 4
gpt4 key购买 nike

我有两个数据库表,如下所示:

CREATE TABLE `customer` (
`id` char(36) NOT NULL,
`name` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `customer_detail` (
`customer_id` char(36) NOT NULL,
`creation_date` date DEFAULT NULL,
`deletion_date` date DEFAULT NULL,
PRIMARY KEY (`customer_id`),
CONSTRAINT `FK_customer_detail_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

和两个映射这些表的域类
class Customer {
String name
String lastname

static hasOne = [detail: CustomerDetail]

static mapping = {
id generator: "assigned"
version false
}

static constraints = {
name maxSize: 50
lastname maxSize: 50
detail nullable: true, unique: true
}
}


class CustomerDetail {
Date creationDate
Date deletionDate

static belongsTo = [customer:Customer]

static mapping = {
id generator: "assigned", column: "customer_id", insert: false, update: false
version false
}
}

当我运行应用程序(运行应用程序)时,出现以下错误:

由MappingException引起:实体的映射中重复的列:my.data.domain.CustomerDetail列:customer_id(应使用insert =“false” update =“false”进行映射)

如果我删除GORM,则在Customer.id和CustomerDetail.id上设置一对一引用,但是
字段customer_detail.id在db中不存在,并且我无法修改db结构。

有什么解决方案?

先感谢您

单核细胞增多症

最佳答案

我在这里找到了解决方案Grails domain-classes mapping in one-to-one relation,所以这些是工作域类:

class Customer {
String id;
String name
String lastname

static hasOne = [detail: CustomerDetail]

static mapping = {
id generator: "assigned"
version false
}

static constraints = {
name maxSize: 50
lastname maxSize: 50
detail nullable: true, unique: true
}
}


class CustomerDetail {
String id;
Date creationDate
Date deletionDate
Customer customer

static mapping = {
id column: '`customer_id`', generator: 'foreign', params: [ property: 'customer'], type: "text"
customer column: '`customer_id`', insertable: false, updateable: false, type: "text"
version false
}
}

关于grails - Grails GORM一对一关系问题:列:(应使用insert =“false” update =“false”进行映射),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24186035/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com