Home > OS >  Why is my Entity class inserting two Id's into my table? Spring Boot JPA
Why is my Entity class inserting two Id's into my table? Spring Boot JPA

Time:04-02

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

CodePudding user response:

I was not aware of @MapsId, learned something new. A simple search and found following can someone please explain me @MapsId in hibernate?

@MapsId("user_id")  //  maps the user id attribute

It may solve your issue. I will try with dummy code if this doesn't solve your issue.

CodePudding user response:

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

  • Related