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

M

mmnnoo

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

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

static constraints = {
}
}

but 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 <id generator: "assigned", column: "customer_id", insert: false, update: false> the 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.

Is there a solutions to that?

Thank you in advance

Mono
 

woodmawa

New member
not sure - i'm not that hot on sql - and your looking at hard mapping from class into db tables - so i'm assuming your doing legacy database mapping approach. however if you look at the static belongsTo = [customer:Customer] - that suggests that there is an attribute called customer in the CustomerDetail table - which there isn't. Also your using a generator for the id - so your getting a new number - therefore its not the same as the id generated in the Customer entity.

if it were me - i'd knock up a couple of classes in gorm and let it do the generation of the tables for you - from your couple of test classes - then look at the generated sql definition and you should be able to figure out where your mapping is going wrong. But it doesn't look right as it stands above
 
Top