0

I am learning Spring and I can't find out how to map two models in Spring JPA. I was able to join table by ID. By default it links user.id. I want to link the user.userName. How to change it? My User model:

@Entity
@Table(name = "user")
public class SiteUser {

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

@Column(name = "username", unique = true, length = 30)
private String userName;

@Column(name = "email", unique = true, length = 60)
private String email;

@Column(name = "password", length = 60)
private String password;

@Column(name = "role", length = 15)
private String role;

@Column(name = "fullname", length = 60)
private String fullName;

@Column(name = "age", length = 3)
private int age;

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public Long getId() {
    return id;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getRole() {
    return role;
}

public void setRole(String role) {
    this.role = role;
}

}

My Profile model:

@Entity
@Table(name="profile")
public class Profile {

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

@OneToOne(targetEntity=SiteUser.class)
@JoinColumn(name="user_id", nullable=false)
private SiteUser user;

@Column(name="about", length=5000)
private String about;

public Long getId() {
    return id;
}

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

public SiteUser getUser() {
    return user;
}

public void setUser(SiteUser user) {
    this.user = user;
}

public void safeCopyFrom(Profile other){

    if(other.about !=null){

        this.about = other.about;
    }

}

}

1 Answers1

0

You are using a bidirectional @OneToOne relation between two entites and where you place your @JoinColumn indicates the owner side of relation that in your case you put it on Profile class. Here is the great article about relationships between two entities, as it mentions

A Join Column in JPA is a column in the owner entity that refers to a key (usually a primary key) in the non-owner or inverse entity. The first thing that comes to mind after reading the above line is that JoinColumns are nothing but a Foreign Key Columns. And it is indeed the case. JPA calls them Join Columns, possibly because they are more verbose in what their actual role is, to join the two entities using a common column.

So you are defining a Foreing Key Column on Profile class The rule of Foreing Key Column is that it should point to a Primary Key or a Unique Column in target entity and you should use it like this on you senario

public class Profile {

    @OneToOne
    @JoinColumn(name="username")
    private SiteUser user;
}