title: Hibernate :Cannot add or update a child row: a foreign key constraint fails
body: <p>i have this issue :</p>
  
  <blockquote>
    <p>Cannot add or update a child row: a
    foreign key constraint fails
    (<code>bytecodete</code>.<code>company_links</code>,
    CONSTRAINT <code>FK_company_links_1</code>
    FOREIGN KEY (<code>id</code>) REFERENCES
    <code>company</code> (<code>idUser</code>) ON DELETE CASCADE
    ON UPDATE CASCADE)</p>
  </blockquote>
  
  <p>But i don't really understand this happens, so i will post my tables here:</p>
  
  <pre><code>//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
     ) 
  </code></pre>
  
  <p>I know this exception gives when i try to insert something in an table where there's a foreign key and i don't have setup some value to this key.
  But this isn't my case, here is my output:</p>
  
  <pre><code>11:47:30,809  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
  Hibernate: 
      insert 
      into
          bytecodete.user
          (email, password, type) 
      values
          (?, ?, ?)
  11:47:30,948 TRACE StringType:133 - binding 'valter.bcc@gmail.com' to parameter: 1
  11:47:30,949 TRACE StringType:133 - binding 'U25YM/DOl5k=' to parameter: 2
  11:47:30,950 TRACE IntegerType:133 - binding '2' to parameter: 3
  Hibernate: 
      insert 
      into
          bytecodete.company
          (cnpj, corporateName, fantasyName, idUser) 
      values
          (?, ?, ?, ?)
  11:47:31,214 TRACE StringType:133 - binding '98806728000100' to parameter: 1
  11:47:31,215 TRACE StringType:133 - binding 'Semana da Computação e Tecnologia' to parameter: 2
  11:47:31,215 TRACE StringType:133 - binding 'SECOT' to parameter: 3
  11:47:31,216 TRACE IntegerType:133 - binding '37' to parameter: 4
  idCompany=37
  Hibernate: 
      select
          company_.idUser,
          company_.cnpj as cnpj9_,
          company_.corporateName as corporat3_9_,
          company_.fantasyName as fantasyN4_9_ 
      from
          bytecodete.company company_ 
      where
          company_.idUser=?
  11:47:31,300 TRACE IntegerType:133 - binding '37' to parameter: 1
  11:47:31,316 TRACE StringType:172 - returning '98806728000100' as column: cnpj9_
  11:47:31,316 TRACE StringType:172 - returning 'Semana da Computação e Tecnologia' as column: corporat3_9_
  11:47:31,351 TRACE StringType:172 - returning 'SECOT' as column: fantasyN4_9_
  Hibernate: 
      insert 
      into
          bytecodete.company_links
          (idCompany, link, serviceName, id) 
      values
          (?, ?, ?, ?)
  11:47:31,353 TRACE IntegerType:133 - binding '37' to parameter: 1
  11:47:31,354 TRACE StringType:133 - binding 'http://www.flickr.com/services/api/' to parameter: 2
  11:47:31,354 TRACE StringType:133 - binding 'Flickr' to parameter: 3
  11:47:31,355 TRACE IntegerType:133 - binding '0' to 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 a child row: a foreign key constraint fails (`bytecodete`.`company_links`, CONSTRAINT `FK_company_links_1` FOREIGN KEY (`id`) REFERENCES `company` (`idUser`) ON DELETE CASCADE ON UPDATE CASCADE)
  </code></pre>
  
  <p>Sorry guys bothering you, but i really need help with this, i don't know what's happen.</p>
  
  <p>Best regards,
  Valter Henrique.</p>