简体   繁体   中英

Grails: how to define a master-detail view with grails constraints

I want to create a simple master/detail view with these tables:

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;

I have these domain classes:

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

But the detail view doesn't assign a foreign key.

What is wrong in my code?


i make this changes

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

}

class DetailId2 implements Serializable {

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

}

but i get this form

Valordet -> edit

Idmaster * -> edit

Master * -> listbox without values

any idea?

Change the mapping of DetailId2 from

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM