user3734260
user3734260

Reputation: 71

Grails GORM One-to-one relation issue: column: (should be mapped with insert="false" update="false")

i've two db tables as follow:

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;

and two domain classes that map these tables

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

when I run the application (run-app) I get the following error:

Caused by MappingException: Repeated column in mapping for entity: my.data.domain.CustomerDetail column: customer_id (should be mapped with insert="false" update="false")

If I remove GORM sets the one-to-one reference on Customer.id and CustomerDetail.id but the field customer_detail.id doesn't exists in db and I can not modify the db structure.

What is the solution to that?

Thank you in advance

Mono

Upvotes: 2

Views: 3544

Answers (1)

user3734260
user3734260

Reputation: 71

I've found the solution here Grails domain-classes mapping in one-to-one relation so these are the working domain classes:

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

Upvotes: 4

Related Questions