java - Hibernate :Cannot add or update a child row: a foreign key constraint fails -


i have issue :

cannot add or update child row: foreign key constraint fails (bytecodete.company_links, constraint fk_company_links_1 foreign key (id) references company (iduser) on delete cascade on update cascade)

but don't understand happens, post tables here:

//user table drop table if exists `bytecodete`.`user`; create table  `bytecodete`.`user` (   `id` int(11) not null auto_increment,   `email` varchar(255) not null,   `password` varchar(255) not null,   `type` tinyint(1) unsigned not null,   primary key (`id`),   unique key `email` (`email`) )  //company table  drop table if exists `bytecodete`.`company`; create table  `bytecodete`.`company` (   `iduser` int(11) not null,   `fantasyname` varchar(55) not null,   `corporatename` varchar(55) not null,   `cnpj` varchar(14) not null,   primary key (`iduser`),   unique key `cnpj` (`cnpj`),   constraint `company_ibfk_1` foreign key (`iduser`) references `user` (`id`)    on delete cascade on update cascade )  // company_links table   drop table if exists `bytecodete`.`company_links`;   create table  `bytecodete`.`company_links` (   `id` int(11) not null default '0',   `idcompany` int(10) unsigned not null,    `servicename` varchar(45) not null,    `link` varchar(45) not null,    primary key (`id`) using btree,    constraint `fk_company_links_1` foreign key (`id`) references `company` (`iduser`)     on delete cascade on update cascade    )  

i know exception gives when try insert in table there's foreign key , don't have setup value key. isn't case, here output:

11:47:30,809  info sessionfactoryobjectfactory:82 - not binding factory jndi, no jndi name configured hibernate:      insert              bytecodete.user         (email, password, type)      values         (?, ?, ?) 11:47:30,948 trace stringtype:133 - binding 'valter.bcc@gmail.com' parameter: 1 11:47:30,949 trace stringtype:133 - binding 'u25ym/dol5k=' parameter: 2 11:47:30,950 trace integertype:133 - binding '2' parameter: 3 hibernate:      insert              bytecodete.company         (cnpj, corporatename, fantasyname, iduser)      values         (?, ?, ?, ?) 11:47:31,214 trace stringtype:133 - binding '98806728000100' parameter: 1 11:47:31,215 trace stringtype:133 - binding 'semana da computação e tecnologia' parameter: 2 11:47:31,215 trace stringtype:133 - binding 'secot' parameter: 3 11:47:31,216 trace integertype:133 - binding '37' parameter: 4 idcompany=37 hibernate:      select         company_.iduser,         company_.cnpj cnpj9_,         company_.corporatename corporat3_9_,         company_.fantasyname fantasyn4_9_              bytecodete.company company_              company_.iduser=? 11:47:31,300 trace integertype:133 - binding '37' parameter: 1 11:47:31,316 trace stringtype:172 - returning '98806728000100' column: cnpj9_ 11:47:31,316 trace stringtype:172 - returning 'semana da computação e tecnologia' column: corporat3_9_ 11:47:31,351 trace stringtype:172 - returning 'secot' column: fantasyn4_9_ hibernate:      insert              bytecodete.company_links         (idcompany, link, servicename, id)      values         (?, ?, ?, ?) 11:47:31,353 trace integertype:133 - binding '37' parameter: 1 11:47:31,354 trace stringtype:133 - binding 'http://www.flickr.com/services/api/' parameter: 2 11:47:31,354 trace stringtype:133 - binding 'flickr' parameter: 3 11:47:31,355 trace integertype:133 - binding '0' parameter: 4 11:47:31,433  warn jdbcexceptionreporter:77 - sql error: 1452, sqlstate: 23000 11:47:31,452 error jdbcexceptionreporter:78 - cannot add or update child row: foreign key constraint fails (`bytecodete`.`company_links`, constraint `fk_company_links_1` foreign key (`id`) references `company` (`iduser`) on delete cascade on update cascade) 

sorry guys bothering you, need this, don't know what's happen.

best regards, valter henrique.

we'd need see code more, based on insert statements, looks maybe foreign key company_links company wrong. insert company_links has 37 idcompany, , 0 id. foreign key set on id, not idcompany, 37 should set on id, or foreign should changed idcompany.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -