为什么我的实体类向我的表中插入两个 Id? Spring Boot JPA

发布于 2025-01-18 12:47:45 字数 2547 浏览 1 评论 0 原文

我有两个实体类用户 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
}

用户表:

user_profile表:

I have two Entity classes User and UserProfile. The User table has a primary key for the User ID which is a long data type. This User ID is supposed to be the primary key in user_profile as well. User also has email as a column, which I want to be linked to the user_profile also. The issue I am having is that for some reason, a column named id is being inserted into my table when I already have the primary key user_id set in the user_profile table. Does anyone know what I am doing wrong?

User:

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
}

User table:
![enter image description here

User_Profile table:
enter image description here

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

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

发布评论

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

评论(2

冷…雨湿花 2025-01-25 12:47:45

您可以避免使用 secondarytable ,并仅使用 oneToOne mappedby @primarykeykoincoincolumn

@Entity
@Table(@Table( name = "users",
   uniqueConstraints = { @UniqueConstraint(columnNames = "username")  }))
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    Long userId;

    @OneToOne(mappedBy = "user")
    UserProfile userProfile;
    // Other fields
    // getters and setters
}

 @Entity
 @Table(name = "user_profile")
 public class UserProfile {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    Long userId;

    @OneToOne
    @PrimaryKeyJoinColumn(name = "user_id")
    User user;

    // other fields
    // getters and setters
}

更多详细信息在此处 https://www.baeldung.com/jpa-mappe-mappe-mapping-single--single--single--实体到阵行表

You can avoid the usage of SecondaryTable and use just OneToOne and mappedBy and @PrimaryKeyJoinColumn :

@Entity
@Table(@Table( name = "users",
   uniqueConstraints = { @UniqueConstraint(columnNames = "username")  }))
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    Long userId;

    @OneToOne(mappedBy = "user")
    UserProfile userProfile;
    // Other fields
    // getters and setters
}

 @Entity
 @Table(name = "user_profile")
 public class UserProfile {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    Long userId;

    @OneToOne
    @PrimaryKeyJoinColumn(name = "user_id")
    User user;

    // other fields
    // getters and setters
}

more details here https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables

も星光 2025-01-25 12:47:45

我不知道@MapsId,学到了一些新东西。
简单搜索发现如下
有人可以向我解释一下休眠中的@MapsId吗?

我尝试过与虚拟代码。看起来我们正在将 @SecondaryTable 和 @MapsId 混合在一起。

重复的列来自SecondaryTable 注释。
我这里没有完整的要求上下文,主要是 https: //www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables 应该可以解决这个需求。 @Oussama ZAGHDOUD 在他的回复中也提到了这一点。

总之,当您想要为 2 个表保留两个单独的实体类并且 @SecondaryTable 将 2 个表合并为一个实体类时,请使用 @MapsId。两者的作用相同,一个表的主键充当第二个表的主键和外键。

仅使用 @MapsId 我得到以下 sql

    Hibernate: create table user_profile (profile_banner varchar(255), profile_img varchar(255), user_user_id bigint not null, primary key (user_user_id))
Hibernate: create table users (user_id bigint generated by default as identity, email varchar(50), password varchar(120), username varchar(20), primary key (user_id))
Hibernate: alter table users add constraint UKr43af9ap4edm43mmtq01oddj6 unique (username)
Hibernate: alter table user_profile add constraint FKqkgvrni6161p23yj2lbp9xxlk foreign key (user_user_id) references users

使用 @SecondaryTable

Hibernate: create table user_profile (email varchar(50), profile_banner varchar(255), profile_img varchar(255), user_id bigint not null, primary key (user_id))
Hibernate: create table users (user_id bigint generated by default as identity, password varchar(120), username varchar(20), primary key (user_id))
Hibernate: alter table users add constraint UKr43af9ap4edm43mmtq01oddj6 unique (username)
Hibernate: alter table user_profile add constraint FKuganfwvnbll4kn2a3jeyxtyi foreign key (user_id) references users

实体类将如下所示

package com.example.demo.entity;

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", pkJoinColumns = @PrimaryKeyJoinColumn(name = "user_id"))
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="user_id")
    private Long userId;

    @Column(name = "username")
    @NotBlank
    @Size(max = 20)
    private String username;

    @Column(name = "email", table = "user_profile")
    @NotBlank
    @Size(max = 50)
    @Email
    private String email;

    @Column(name = "profile_img", table = "user_profile") //S3 image Link
    private String profile_img;

    @Column(name = "profile_banner", table = "user_profile") //S3 image Link
    private String profile_banner;

    @NotBlank
    @Size(max = 120)
    private String password;

    //getter methods
    //setter methods
}

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

    Hibernate: create table user_profile (profile_banner varchar(255), profile_img varchar(255), user_user_id bigint not null, primary key (user_user_id))
Hibernate: create table users (user_id bigint generated by default as identity, email varchar(50), password varchar(120), username varchar(20), primary key (user_id))
Hibernate: alter table users add constraint UKr43af9ap4edm43mmtq01oddj6 unique (username)
Hibernate: alter table user_profile add constraint FKqkgvrni6161p23yj2lbp9xxlk foreign key (user_user_id) references users

With @SecondaryTable

Hibernate: create table user_profile (email varchar(50), profile_banner varchar(255), profile_img varchar(255), user_id bigint not null, primary key (user_id))
Hibernate: create table users (user_id bigint generated by default as identity, password varchar(120), username varchar(20), primary key (user_id))
Hibernate: alter table users add constraint UKr43af9ap4edm43mmtq01oddj6 unique (username)
Hibernate: alter table user_profile add constraint FKuganfwvnbll4kn2a3jeyxtyi foreign key (user_id) references users

Entity class will look like below

package com.example.demo.entity;

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", pkJoinColumns = @PrimaryKeyJoinColumn(name = "user_id"))
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="user_id")
    private Long userId;

    @Column(name = "username")
    @NotBlank
    @Size(max = 20)
    private String username;

    @Column(name = "email", table = "user_profile")
    @NotBlank
    @Size(max = 50)
    @Email
    private String email;

    @Column(name = "profile_img", table = "user_profile") //S3 image Link
    private String profile_img;

    @Column(name = "profile_banner", table = "user_profile") //S3 image Link
    private String profile_banner;

    @NotBlank
    @Size(max = 120)
    private String password;

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