Hibernate:无法添加或更新子行:外键约束失败

发布于 2024-10-31 17:50:10 字数 4014 浏览 0 评论 0原文

我有这个问题:

无法添加或更新子行:a 外键约束失败 (字节码company_links, 约束 FK_company_links_1 外键 (id) 参考 company (idUser) ON 删除级联 关于更新级联)

但我真的不明白这种情况发生,所以我将在这里发布我的表格:

//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
   ) 

我知道当我尝试在有外键的表中插入某些内容并且我没有设置一些值时会出现此异常到这个键。 但这不是我的情况,这是我的输出:

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 '[email protected]' 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)

对不起,打扰你们了,但我真的需要帮助,我不知道发生了什么。

此致, 瓦尔特·恩里克.

i have this issue :

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)

But i don't really understand this happens, so i will post my 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 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:

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 '[email protected]' 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)

Sorry guys bothering you, but i really need help with this, i don't know what's happen.

Best regards,
Valter Henrique.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-11-07 17:50:10

我们可能需要查看一些代码来为您提供更多帮助,但仅根据 INSERT 语句,您从 company_linkscompany 的外键可能是错误的。您插入 company_links 时,idCompany 为 37,id 为 0。您的外键设置在 id 上,而不是 idCompany,因此 37 应设置在 id 上,或者外键应更改为 <代码>idCompany。

We'd probably need to see some code to help you more, but just based on the INSERT statements, it looks like maybe your foreign key from company_links to company is wrong. Your INSERT into company_links has a 37 for idCompany, and a 0 for id. Your foreign key is set up on id, not idCompany, so the 37 should be set on id, or the foreign should be changed to idCompany.

巨坚强 2024-11-07 17:50:10

看来您的参数顺序混淆了。您的插入语句是:

<前><代码>插入
进入
字节码.company_links
(id公司、链接、服务名称、id)
价值观
(?, ?, ?, ?)

您将 37 绑定到参数 1 (idCompany),将 0 绑定到参数 4 (id)。 id 需要是 37,而不是 0。

Looks like you have your parameter order mixed up. Your insert statement is:

insert 
into
    bytecodete.company_links
    (idCompany, link, serviceName, id) 
values
    (?, ?, ?, ?)

And you are binding 37 to parameter 1 (idCompany), and 0 to parameter 4 (id). The id needs to be 37, not 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文