1

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

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}" 
    }
}

但是详细视图没有分配外键。

我的代码有什么问题?


我做这个改变

类 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 * -> 没有值的列表框

任何想法?

4

1 回答 1

0

将 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
    }
于 2012-12-18T06:01:27.020 回答