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

Issue

I have a database table named user that has a Primary key id. I also have another database table named user_profile which has a primary key of user_id. I want the user_id column to be linked with the primary key id which is in my user table but don’t know how to do it. I know it involves making a foreign key that links with the primary key, and that it has something to do with @JoinTable or @JoinColumn, but am not sure what to do. If anyone could help me, I would deeply appreciate it.

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;
        }
    }

Solution

The trick would be to use @MapsId on the User reference inside UserProfile. This will map your User id primary key column to UserProfile user_id.

User stays the same:

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

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

        // skipped code
}

The UserProfile id column does not need @GeneratedValue because the id will be populated with the id of the User entity. Hibernate will expect a user_id column inside user_profile table:

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

     @Id
     private Long id;

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

     // skipped code
}

Instead of having a column named user_id inside user_profile table you can have it named id, but to do that you need to add a JoinColumn(name = "id") on your User reference:

@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
}

Answered By – happy songs

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.