1

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

1 Answer 1

2

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
}
2
  • Awesome! Would this be possible to do so that my user_profile's primary key is also user_id? Rather than have it as its own ID and a separate column for user_id? I know I stated I neeed user_id to be a foreign key, but in this case I think it may be better if its both primary and foreign if thats even possible. So in other words, can primary key 'id' in 'user' table be linked with another primary key 'user_id' in the 'user_profile' table? Mar 24, 2022 at 9:59
  • Yes, the first 2 code snippets do that: you will have users table with primary key id and user_profile table with primary key user_id which references the parent users. The third code snippet is in case you want to rename user_id to id, but if you want it named user_id you can ignore it Mar 24, 2022 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.