`
bnmnba
  • 浏览: 288918 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

hibernate报错:Cannot add or update a child row: a foreign key constraint fails

 
阅读更多

我遇到这个问题的原因是:把主键作为外键关联到了其他表的主键。

在实体Product:

 

package pp.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)  
@DiscriminatorValue("product")
public class Product extends Goods{

	@OneToMany(targetEntity=ProductAssembleRecord.class,fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.MERGE})
	@JoinColumn(name="productAssembleId",insertable=true,updatable=true)//成功的
	private List<ProductAssembleRecord> productAssembleRecords=new ArrayList<ProductAssembleRecord>();
	//.....
	
}

 配置一对多时:@JoinColumn(name="productAssembleRecordId",insertable=true,updatable=true)(错误的。)

 

 

实体ProductAssembleRecord:

 

@Entity
@Table
public class ProductAssembleRecord implements IEntity {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int  productAssembleRecordId;
//......
}
 

看起来这样的配置会导致ProductAssembleRecord的主键productAssembleRecordId会作为外键映射到实体Product的主键goodsId(这个是其父类的主键,同一个表所以共享,不用管它)。我如果插入第一个ProductAssembleRecord没有问题,因为productAssembleRecordId能找到一个goodsId(只有一条数据)来外键关联。第二次就会失败,因为主键会自动增长。

解决:把@JoinColumn(name="productAssembleRecordId",insertable=true,updatable=true)

换做:@JoinColumn(name="productAssembleId",insertable=true,updatable=true)

从新建立表结构。ok。实体ProductAssembleRecord对应的表多了一个叫productAssembleId列(我的表结构是用实体生成的)。

 

分享到:
评论

相关推荐

    解决django 新增加用户信息出现错误的问题

    (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) ...

    MySQL删除表的时候忽略外键约束的简单实现

    ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails 这是因为你尝试删除的表中的字段被用作了其他表的外键,因此在删除这个表(父表)之前必须先删除具有外键的表(子表)...

    MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法

    这篇文章主要涉及到在数据创建表时,遇到ERROR 1215 (HY000): Cannot add foreign key constraint 问题方面的内容,对于在数据创建表时,遇到同样问题感兴趣的同学可以参考一下。 一、问题的提出 创建两个表:  ...

    Navicat删除行时报Cannot delete or update a parent row: a foreign key constraint fails

    在SSM项目中执行一个删除用户操作时报错,遂在navicat中尝试是否可以直接删除,报如下所示错误 student表的主键是selectedcourse表的外键,当需要删除student表内的一行数据时,必须在selectedcourse表内设置该外键...

    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘a3b6420a-6’ for key ‘callId’

    "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'a3b6420a-6724-11ea-b2a3-d773d1d6999f' for key 'callId'\nThe SQL being executed was: INSERT INTO `ly_call` (`call_id`, `mobile`, ...

    sql关闭与开启

    Cannot delete or update a parent row: a foreign key constraint fails (...) 这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。 SET ...

    constraint-layout-solver-1.0.0-alpha1

    Failed to resolve: com.android.support.constraint:constraint-layout:1.0.0-alpha1

    SSD7 选择题。Multiple-Choice

    The foreign key in a table T1 _____ the same _____ as the corresponding primary key in table T2. must have, name need not have, name must have, domain (a) I, II, and III (b) I and II (c) ...

    课程设计—仓库设备管理

    课程设计—仓库设备管理系统.doc 创建基本表的SQL代码: create table Equipment ( ...alter table Stock add constraint Eno3 foreign key(Eno) references Equipment(Eno) on update cascade on delete cascade

    INSERT语句与FOREIGN KEY约束冲突

    阅读此书以了解根本原因:SQL FOREIGN KEY Constraint [^]

    T6 5.1升级T66.2报错

    ALTER TABLE [dbo].[IA_Subsidiary] ADD CONSTRAINT [DF__ia_subsid__cDefi__12FE9D09] DEFAULT (NULL) FOR [cDefine24] GO /****** Object: Default [DF__ia_subsid__cDefi__13F2C142] Script Date: 06/12/2014 14:...

    详细oracle笔记1

    alter table table_name add constraint fk_name foreign key (table_column) references key_table_name; 3.使主键或外键失效、生效 alter table table_name disable(enable) constraint key_name;

    spring mvc 校验

    springmvc 校验时所需要的三个包validation-api.jar hibernate-validator-4.1final.jar jbosslogin.jar !解决了tomcat启动的时候报错: classnotfound:javax.validation.constraint的错误!

    MySQL删除有外键约束的表数据方法介绍

    aforeignkeyconstraintfails (...) 这是因为MySQL中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。 禁用外键约束,我们可以使用: SETFOREIGN_KEY_CHECKS=...

    级联删除笔记【自用0分】

    foreign key(AID) references dbo.A(AID) on delete cascade on update cascade go alter table dbo.C add constraint FK_C_B_BID foreign key(BID) references dbo.B(BID) on delete cascade on update cascade go ...

    python-mysql day05.txt

    constraint 外键名 foreign key(字段) references 主表(字段) on delete 级联动作 on update 级联动作 3、级联动作 1、cascade :删除、更新同步(被参考字段) 2、restrict :不让主表更新、删除 3、set null...

    聊天室系统数据库设计.txt

    gruopid int CONSTRAINT g_FORE FOREIGN KEY REFERENCES user_group(groupid), ) CREATE TABLE chat_log ( logid int CONSTRAINT l_PRIM PRIMARY KEY, senderid int CONSTRAINT s_FORE FOREIGN KEY REFERENCES ...

    Object Constraint Language, The: Getting Your Models Ready for MDA, Second

    "In this thoroughly revised edition, Jos and Anneke offer a concise, pragmatic, and pedagogic explanation of the Object Constraint Language (OCL) and its different applications. Their discussion of ...

    Object Constraint Language, The: Getting Your Models Ready for MDA, Second Edition

    "In this thoroughly revised edition, Jos and Anneke offer a concise, pragmatic, and pedagogic explanation of the Object Constraint Language (OCL) and its different applications. Their discussion of ...

Global site tag (gtag.js) - Google Analytics