SpringDataJPA保存OneToOne关系获取无法添加或更新子行:外键约束失败 [英] SpringDataJPA save OneToOne relationship getting Cannot add or update a child row: a foreign key constraint fails

查看:2421
本文介绍了SpringDataJPA保存OneToOne关系获取无法添加或更新子行:外键约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有USER(id)和CONTACT(user_id,first,last)表。 CONTACT.user_id是USER表的外键。



在User.java中:

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

//双向一对一关联联系
@OneToOne(mappedBy =user,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
私人联系联系人;

在Contact.java中:



<$ p $
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =USER_ID)
private int userId;

//双向一对一关联到用户
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name =User_id)
私人用户用户;

当我运行userRepository.save(user)时,我得到:

$ b $ [EclipseLink-4002](Eclipse持久性服务 - 2.5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.DatabaseException内部异常:com [b]

 [code] .mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(`mysite`.`contact`,CONSTRAINT`fk_CONTACT_USER1` FOREIGN KEY(`USER_ID`)REFERENCES`user`(` ID`)ON DELETE NO ACTION ON UPDATE NO ACTION)

我做错了什么?感谢!

解决方案

问题可能是您的id生成策略(只要您正确初始化了联系人)。
必须将用户对象插入数据库才能设置其ID,但同时联系人需要此ID为有效对象。
它们都必须发生在同一个事务中。



如果您将日志记录级别更改为JPA(在perstitance.xml中),那么您将最可能会看到插入用户和联系人的顺序,但联系人将具有user_id 0。



因此,a)确保您在联系人中显式设置用户(如您所说关系由联系人管理。b)设置用户的联系人。 c)坚持(在一次交易中)。
根据您的JPA实现,它可能仍然不起作用(检查发出的查询,最有可能在联系人上插入然后更新)。将生成策略更改为TABLE,使用TABLE JPA获取下一个空闲ID,将其分配给该对象,并执行插入,以便在插入之前知道插入。


I have USER(id) and CONTACT(user_id, first, last) table. CONTACT.user_id is the foreign key to USER table.

In User.java:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

//bi-directional one-to-one association to Contact
@OneToOne(mappedBy="user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)   
private Contact contact; 

In Contact.java:

    @Id
//  @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="USER_ID")
    private int userId;

//bi-directional one-to-one association to User
@OneToOne(cascade = CascadeType.ALL )
@PrimaryKeyJoinColumn(name = "User_id")
private User user;

When I run userRepository.save(user), I am getting:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mysite`.`contact`, CONSTRAINT `fk_CONTACT_USER1` FOREIGN KEY (`USER_ID`) REFERENCES `user` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)  

What did I do wrong? Thanks!

解决方案

The problem is probably your id generation strategy (provided you initialized the contact properly). The user object must be inserted to DB to have its id set, but at the same time the Contact needs this id to be valid object. They both have to happen in the same transaction.

If you switch the logging level to fine for your JPA (in perstitance.xml), you will most likely see the sequence of inserting the user and contact but contact will have user_id 0.

So, a) make sure you explicit set user in your contact (as you said the relation is managed by Contact. b) Set contact on the user. c) persist (in one transaction). Depending on your JPA implementation it may still not work (check the queries issued, most likely there will be insert on contact and then update). Change the genration strategy to TABLE, with TABLE JPA takes the next free ID, assignes it to the object and does the insert so it is "known" before the insert.

这篇关于SpringDataJPA保存OneToOne关系获取无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆