JPA多对多配置
目录
1表结构 1
1.1角色表 1
1.2用户表 1
1.3角色用户表 2
2单向多对多 2
2.1角色实体类 2
2.2用户实体类 5
3双向多对多 9
create table `sys_role` (
`role_id` double ,
`create_time` datetime ,
`remark` varchar (765),
`role_code` varchar (765),
`role_desc` varchar (765),
`role_name` varchar (765),
`role_state` bit (1)
);
create table `sys_user` (
`id` double ,
`chinese_name` varchar (765),
`create_time` datetime ,
`email` varchar (765),
`password` varchar (765),
`phone` varchar (765),
`remark` varchar (765),
`user_state` bit (1),
`username` varchar (765)
);
create table `sys_role_user` (
`role_user_id` double ,
`role_id` double ,
`user_id` double
);
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* 角色实体类
*
* @author 刘宏强
* @date 2018年3月30日
* @version 1.0
*/
@Entity
public class SysRole implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long roleId;
/**
* 角色名称
*/
private String roleName;
/**
* 角色编码
*/
private String roleCode;
/**
* 角色描述
*/
private String roleDesc;
/**
* 创建时间
*/
private Timestamp createTime;
/**
* 角色状态
*/
private Boolean roleState;
/**
* 备注
*/
private String remark;
public SysRole() {
}
public SysRole(String roleName, String roleCode, String roleDesc, Timestamp createTime, Boolean roleState,
String remark) {
this.roleName = roleName;
this.roleCode = roleCode;
this.roleDesc = roleDesc;
this.createTime = createTime;
this.roleState = roleState;
this.remark = remark;
}
public Long getRoleId() {
return roleId;
}
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public Boolean getRoleState() {
return roleState;
}
public void setRoleState(Boolean roleState) {
this.roleState = roleState;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
/**
* 用户实体类
*
* @author 刘宏强
* @date 2018年3月30日
* @version 1.0
*/
@Entity
public class SysUser implements Serializable {
/**
* 用户主键
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 中文名
*/
private String chineseName;
/**
* 手机
*/
private String phone;
/**
* 电子邮箱
*/
private String email;
/**
* 创建时间
*/
private Timestamp createTime;
/**
* 用户状态
*/
private Boolean userState;
/**
* 备注
*/
private String remark;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "sys_role_user", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = {
@JoinColumn(name = "user_id") })
private Set
public SysUser() {
}
public SysUser(String username, String password, String chineseName, String phone, String email,
Timestamp createTime, Boolean userState, String remark, Set
this.username = username;
this.password = password;
this.chineseName = chineseName;
this.phone = phone;
this.email = email;
this.createTime = createTime;
this.userState = userState;
this.remark = remark;
this.sysRoles = sysRoles;
}
public String getUsername() {
return username;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getChineseName() {
return chineseName;
}
public void setChineseName(String chineseName) {
this.chineseName = chineseName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public Boolean getUserState() {
return userState;
}
public void setUserState(Boolean userState) {
this.userState = userState;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Set
return sysRoles;
}
public void setSysRoles(Set
this.sysRoles = sysRoles;
}
}
略