jpa構成OneToOne関係
5926 ワード
ホルモンはuserテーブルとroleテーブルを1対1の関係に配置します
roleテーブルでも同様に設定
@Entity
@Table(name = "tb_user")
@SequenceGenerator(name = "tb_user_sq",sequenceName = "tb_user_sqe")
public class TbUser extends BaseEntity{
/**
*
*/
private String userName;
/**
*
*/
private String loginName;
/**
*
*/
private String passWord;
/**
*
*/
private String telPhone;
/**
*
*/
private TbRole tbRole;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getTelPhone() {
return telPhone;
}
public void setTelPhone(String telPhone) {
this.telPhone = telPhone;
}
@Id
@Override
@GeneratedValue(generator = "tb_user_sq",strategy = GenerationType.SEQUENCE)
public Long getId() {
return this.id;
}
@OneToOne
@JoinColumn(name = "tb_role")
public TbRole getTbRole() {
return tbRole;
}
public void setTbRole(TbRole tbRole) {
this.tbRole = tbRole;
}
}
roleテーブルでも同様に設定
@Entity
@Table(name = "tb_role")
@SequenceGenerator(name = "tb_role_sq",sequenceName = "tb_role_sqe")
public class TbRole extends BaseEntity{
@Override
@Id
@GeneratedValue(generator = "tb_role_sq",strategy = GenerationType.SEQUENCE)
public Long getId() {
return this.id;
}
private String roleName;
private TbUser tbUser;
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@OneToOne
@JoinColumn(name = "tb_user")
public TbUser getTbUser() {
return tbUser;
}
public void setTbUser(TbUser tbUser) {
this.tbUser = tbUser;
}
}