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