gpt4 book ai didi

java - Grails:如何使用 grails 约束定义主从 View

转载 作者:行者123 更新时间:2023-12-01 04:57:07 25 4
gpt4 key购买 nike

我想使用这些表创建一个简单的主/详细 View :

create table MASTER_ID2
(
ID int not null,
VALOR varchar(40),
primary key (ID)
);

create table DETAIL_ID2
(
ID int not null,
ID_MASTER int not null,
VALOR_DET char(40),
primary key (ID)
);

alter table DETAIL_ID2 add constraint FK_RDET5 foreign key (ID_MASTER)
references MASTER_ID2 (ID) on delete restrict on update restrict;

我有这些域类:

class MasterId2 {

Integer id
String valor
//
static hasMany = [details : DetailId2]

static mapping = {
table 'master_id2'
version false
id generator:'identity', column:'ID'
//
details column: 'id_master'
}

static constraints = {
id(max: 2147483647)
valor(size: 0..40)
}

String toString() {
return "${id}"
}
}

class DetailId2 implements Serializable {

Integer id
Integer id_master
String valor_det
//
MasterId2 master
static belongsTo = MasterId2

static mapping = {
table 'detail_id2'
version false
id generator:'identity', column:'ID'
}

static constraints = {
id(max: 2147483647)
valor_det(size: 0..40)
}

String toString() {
return "${id}"
}
}

但是详细信息 View 没有分配外键。

我的代码有什么问题?

<小时/>

我做了这个改变

MasterId2 类{

Integer id
String valor
//
static hasMany = [details : DetailId2]

static mapping = {
table 'master_id2'
// version is set to false, because this isn't available by default for legacy databases
version false
id generator:'identity', column:'ID'
//
details column: 'id_master'

}

static constraints = {
id(max: 2147483647)
valor(size: 0..40)
}
String toString() {
return "${id}"
}

}

类 DetailId2 实现可序列化{

Integer id
Integer id_master
String valor_det
//
//MasterId2 master
//static belongsTo = MasterId2
static belongsTo = [master: MasterId2]

static mapping = {
table 'detail_id2'
// version is set to false, because this isn't available by default for legacy databases
version false
id generator:'identity', column:'ID'
//
master insertable: false // enforce foreign key
master updateable: false // enforce foreign key

}

static constraints = {
id(max: 2147483647)
valor_det(size: 0..40)
}
String toString() {
return "${id}"
}

}

但我收到了这张表格

瓦洛代 -> 编辑

Idmaster * -> 编辑

Master * -> 没有值的列表框

有什么想法吗?

最佳答案

更改 DetailId2 的映射

static mapping = {
table 'detail_id2'
version false
id generator:'identity', column:'ID'
}

static mapping = {
table 'detail_id2'
version false
id generator:'identity', column:'ID'
master insertable: false // enforce foreign key
master updateable: false // enforce foreign key
}

关于java - Grails:如何使用 grails 约束定义主从 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13921944/

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