Grails GORM一对一关系问题:列:(应该用insert =&#34映射; false" update =" false")

时间:2014-06-12 13:43:18

标签: grails gorm

我有两个数据库表如下:

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

当我运行应用程序(run-app)时,我收到以下错误:

由MappingException引起:实体映射中的重复列:my.data.domain.CustomerDetail列:customer_id(应使用insert =&#34映射; false" update =" false")

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

解决方法是什么?

提前谢谢

1 个答案:

答案 0 :(得分:4)

我在这里找到了解决方案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
    }
}