为什么我的实体类向我的表中插入两个 Id? Spring Boot JPA
我有两个实体类用户
和 userProfile
。 用户
表具有用于用户ID的主要键,该键是长数据类型。该用户ID也应该是USER_PROFILE中的主要键。 用户
也有电子邮件作为列,我也想链接到user_profile。我遇到的问题是,由于某种原因,当我已经在user_profile表中设置了主密钥user_id时,将插入名为ID的列插入。有人知道我在做什么错吗?
用户:
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Entity
@Table( name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
})
@SecondaryTable(name = "user_profile")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
@NotBlank
@Size(max = 20)
private String username;
@Column(name = "email", table = "user_profile")
@NotBlank
@Size(max = 50)
@Email
private String email;
@NotBlank
@Size(max = 120)
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable( name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
//getter methods
//setter methods
}
user_profile:
import javax.persistence.*;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private User user;
// @JoinColumn(name = "email")
// @OneToOne(fetch = FetchType.LAZY)
// private User email;
@Column(name = "profile_img") //S3 image Link
private String profile_img;
@Column(name = "profile_banner") //S3 image Link
private String profile_banner;
//getter methods
//setter methods
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以避免使用
secondarytable
,并仅使用oneToOne
和mappedby
和@primarykeykoincoincolumn
:更多详细信息在此处 https://www.baeldung.com/jpa-mappe-mappe-mapping-single--single--single--实体到阵行表
You can avoid the usage of
SecondaryTable
and use justOneToOne
andmappedBy
and@PrimaryKeyJoinColumn
:more details here https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables
我不知道@MapsId,学到了一些新东西。
简单搜索发现如下
有人可以向我解释一下休眠中的@MapsId吗?
我尝试过与虚拟代码。看起来我们正在将 @SecondaryTable 和 @MapsId 混合在一起。
重复的列来自SecondaryTable 注释。
我这里没有完整的要求上下文,主要是 https: //www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables 应该可以解决这个需求。 @Oussama ZAGHDOUD 在他的回复中也提到了这一点。
总之,当您想要为 2 个表保留两个单独的实体类并且 @SecondaryTable 将 2 个表合并为一个实体类时,请使用 @MapsId。两者的作用相同,一个表的主键充当第二个表的主键和外键。
仅使用 @MapsId 我得到以下 sql
使用 @SecondaryTable
实体类将如下所示
I was not aware of @MapsId, learned something new.
A simple search and found following
can someone please explain me @MapsId in hibernate?
I tried with dummy code. It looks like we are mixing @SecondaryTable and @MapsId together.
The duplicate column is from SecondaryTable annotation.
i don't have full context of requirement here, mostly https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables should solve the need. @Oussama ZAGHDOUD also mentioned this in his response.
In summary, use @MapsId when you want to keep two separate entity class for 2 tables and @SecondaryTable combines 2 tables into one entity class. Both acts same way, primary key of one table works as primary key and foreign key of 2nd table.
With @MapsId only I was getting following sql
With @SecondaryTable
Entity class will look like below