如何将另一个 table 中的用户 ID 主键 link 到 user_id 作为外键?

How can I make User ID primary key link to user_id in another table as a foreign key?

我有一个名为 user 的数据库 table,它有一个主键 id。我还有另一个名为 user_profile 的数据库 table,它的主键是 user_id。我希望 user_id 列与我的用户 table 中的主键 id 相关联,但不知道该怎么做。我知道它涉及制作一个与主键链接的外键,并且它与 @JoinTable@JoinColumn 有关,但我不确定该怎么做。如果有人能帮助我,我将不胜感激。

UserProfile.java:

    import net.bytebuddy.dynamic.loading.InjectionClassLoader;
    import org.springframework.web.multipart.MultipartFile;

    import javax.persistence.*;

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

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

    //    @OneToOne
    //    @JoinColumn(table = "users",  name = "username")
    ////    @PrimaryKeyJoinColumn(name = "profile_id")
    //    User user;


        @Column(name = "profile_img")
        private String profile_img;

        @Column(name = "profile_banner")
        private String profile_banner;

        @Column(name = "user_alias")
        private String user_alias;




    //
    //    public void setUsers(User users) {
    //        this.user = users;
    //    }
    //
    //    public User getUser() {
    //        return user;
    //    }

        public Long getProfileId() {
            return id;
        }

        public void setProfileId(Long id) {
            this.id = id;
        }



        public String getProfile_img() {
            return profile_img;
        }

        public String getProfile_img_complete() {
            return  profile_img;
        }

        public void setProfile_img(String profile_img) {
            this.profile_img = profile_img;
        }

        public String getProfile_banner() {
            return profile_banner;
        }

        public String getProfile_banner_complete() {
            return profile_banner;
        }

        public void setProfile_banner(String profile_banner) {
            this.profile_banner = profile_banner;
        }

        public String getUser_alias() {
            return user_alias;
        }

        public void setUser_alias(String user_alias) {
            this.user_alias = user_alias;
        }

    }

User.java:

    import javax.persistence.*;
    import javax.validation.constraints.Email;
    import javax.validation.constraints.NotBlank;
    import javax.validation.constraints.Size;
    import java.util.HashSet;
    import java.util.Set;
    import com.user.UserProfile;

    @Entity
    @Table( name = "users",
            uniqueConstraints = {
                    @UniqueConstraint(columnNames = "username")

            })
    public class User {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @NotBlank
        @Size(max = 50)
        private String username;
        @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<>();



        public User(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public User() {

        }
    //
    //    @OneToOne(mappedBy = "email")
    //    UserProfile userProfile;

    //    public Long getId() {
    //        return id;
    //    }
    //
    //    public void setId(Long id) {
    //        this.id = id;
    //    }
    //
    //    public UserProfile getUserProfile() {
    //        return userProfile;
    //    }
    //
    //    public void setUserProfile(UserProfile userProfile) {
    //        this.userProfile = userProfile;
    //    }
    public Long getId() {
        return id;
    }
        public void setId(Long id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
        public Set<Role> getRoles() {
            return roles;
        }
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
    }

诀窍是在 UserProfile 内的 User 引用上使用 @MapsId。这会将您的 User id 主键列映射到 UserProfile user_id.

User保持不变:

@Entity
@Table(name = "users")
public class User {

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

        // skipped code
}

UserProfile id 列不需要 @GeneratedValue 因为 id 将填充 User 实体的 id。 Hibernate 将在 user_profile table:

中期望一个 user_id
@Entity
@Table(name = "user_profile")
public class UserProfile {

     @Id
     private Long id;

     @OneToOne(fetch = FetchType.LAZY)
     @MapsId
     private User user;

     // skipped code
}

您可以将其命名为 id,而不是在 user_profile user_id 中添加一个名为 user_id 的列,但是您需要添加一个 [=26] =] 在你的 User 参考资料上:

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

     @Id
     private Long id;

     @OneToOne(fetch = FetchType.LAZY)
     @MapsId
     @JoinColumn(name = "id")
     private User user;

     // skipped code
}