简体   繁体   English

Java JPA-更新后OneToOne关系中断

[英]Java JPA - OneToOne relation breaks after updating

I have two tables (entities) named User and Profile in JPA what are syncing with MySQL database. 我在JPA中有两个名为UserProfile表(实体),它们正在与MySQL数据库同步。 Relation between them is OneToOne and bidirectional. 它们之间的关系是OneToOne和双向的。

Here is how I made relation in model: 这是我在模型中建立关系的方式:

User: 用户:

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
private Profile profile;

Profile: 轮廓:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "profile", optional = false)
private User user;

I am building REST API and my problem is when I update profile by his ID from controller. 我正在构建REST API,而我的问题是当我通过控制器中的ID更新配置文件时。

Before I update data in Profile I am able to getUser from this profile. 在更新Profile数据之前,我可以从该配置文件中获取getUser

After I update data in Profile (by profileService.update(profile); ) I am not able to getUser for this profile. 在更新Profile数据之后(通过profileService.update(profile); ),我无法为此配置文件获取getUser

Seems like the relation was broken. 似乎关系破裂了。

In database there is no USER_ID on Profile table, just table 'User' have PROFILE_ID column. 在数据库中,“ Profile表上没有USER_ID,只有表“用户”具有PROFILE_ID列。

Relation is working fine until I update it. 关系在我更新之前可以正常工作。

I fixed problem. 我解决了问题。 It was caused by not setting User into Profile before updating, because 'dao.update()' just replacing data, not actually updating them. 这是由于更新之前未将用户设置为个人资料引起的,因为'dao.update()'只是替换数据,而不是实际更新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM