Springboot-多表关联的建立(中间表)

场景:用户表、项目表、用户和项目中间表

 

多对多表的实体创建,记得在配置文件中加:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

MyISAM和InnoDB详解:https://blog.csdn.net/shuaijunlan/article/details/51519702

 

ProjectUser表的实体代码:

@Entity
@Table(name="project_has_user", schema = "表名")
@IdClass(ProjectUserKeys.class)
public class ProjectUser implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@ManyToOne(targetEntity = Project.class,fetch=FetchType.EAGER)
	@Id private Project project;
	
	@ManyToOne(targetEntity = User.class,fetch=FetchType.EAGER)
	@Id private User user;

	public Project getProject() {
		return project;
	}

	public void setProject(Project projectId) {
		this.project = projectId;
	}
	
	public User getUser() {
		return user;
	}

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

	public ProjectUser() {
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((project == null) ? 0 : project.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProjectUser other = (ProjectUser) obj;
		if (project == null) {
			if (other.project != null)
				return false;
		} else if (!project.equals(other.project))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}
}

 

ProjectUserKeys表的实体代码:

//Project_User的复合主键类
public class ProjectUserKeys implements Serializable {
    private String user;
    private String project;

    public ProjectUserKeys() {
    }

    public ProjectUserKeys(String userId, String projectId) {
        this.user = userId;
        this.project = projectId;
    }

    //Setter and Getter
    public String getUserId() {
        return user;
    }

    public void setUserId(String userId) {
        this.user = userId;
    }

    public String getProjectId() {
        return project;
    }

    public void setProjectId(String projectId) {
        this.project = projectId;
    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((user == null) ? 0 : user.hashCode());
        result = PRIME * result + ((project == null) ? 0 : project.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(getClass() != obj.getClass()){
            return false;
        }

        final ProjectUserKeys other = (ProjectUserKeys)obj;
        if(user == null){
            if(other.user != null){
                return false;
            }
        }else if(!user.equals(other.user)){
            return false;
        }
        if(project == null){
            if(other.project != null){
                return false;
            }
        }else if(!project.equals(other.project)){
            return false;
        }

        return true;
    }
}

 

 

 

Project表的实体代码:

@Entity
@Table(name = "project", schema = "表名")
public class Project implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(length=36)
	private String id;
	
	@Column(columnDefinition="varchar(45) COMMENT '项目名称'" , nullable=false)
	private String name;			//项目名称
	
	@Lob
	@Column(columnDefinition="TEXT COMMENT '项目描述'")//@Column(columnDefinition="COMMENT '项目描述'")
	private String  description;	//项目描述
	
	@Column(columnDefinition="varchar(36) COMMENT '上级项目'")
	private String pid;				//上级项目
	@Column(columnDefinition="varchar(36) COMMENT '项目负责人'")
	private String leader;			//项目负责人
	
	@Column(columnDefinition="INT NULL DEFAULT 0 COMMENT '状态:\\n-2:失败\\n-1:删除\\n0:未设置\\n1:进行中\\n2:完成\\n3:暂停'")
	private Integer status;				//状态:\n-2:失败\n-1:删除\n0:未设置\n1:进行中\n2:完成\n3:暂停',
		
	@Temporal(TemporalType.DATE)
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name="approval_date",columnDefinition="DATE COMMENT '获批时间'")
	private Date approvalDate;	//获批时间
	
	
	@Temporal(TemporalType.DATE)
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name="check_date",columnDefinition="DATE COMMENT '验收时间'")
	private Date checkDate;    //验收时间
	
	@Column(columnDefinition="TEXT COMMENT '预期成果'")
	private String achievement;
	
	@OneToMany(mappedBy = "project",fetch=FetchType.EAGER)
	private List projectUser;

	public Project() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Project(String id, String name, String description, String pid, String leader, Integer status,
			Date approvalDate, Date checkDate, String achievement, List projectUser) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.pid = pid;
		this.leader = leader;
		this.status = status;
		this.approvalDate = approvalDate;
		this.checkDate = checkDate;
		this.achievement = achievement;
		this.projectUser = projectUser;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public String getPid() {
		return pid;
	}

	public String getLeader() {
		return leader;
	}

	public Integer getStatus() {
		return status;
	}

	public Date getApprovalDate() {
		return approvalDate;
	}

	public Date getCheckDate() {
		return checkDate;
	}

	public String getAchievement() {
		return achievement;
	}

	public List getProjectUser() {
		return projectUser;
	}

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

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public void setLeader(String leader) {
		this.leader = leader;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setApprovalDate(Date approvalDate) {
		this.approvalDate = approvalDate;
	}

	public void setCheckDate(Date checkDate) {
		this.checkDate = checkDate;
	}

	public void setAchievement(String achievement) {
		this.achievement = achievement;
	}

	public void setProjectUser(List projectUser) {
		this.projectUser = projectUser;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((achievement == null) ? 0 : achievement.hashCode());
		result = prime * result + ((approvalDate == null) ? 0 : approvalDate.hashCode());
		result = prime * result + ((checkDate == null) ? 0 : checkDate.hashCode());
		result = prime * result + ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((leader == null) ? 0 : leader.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((pid == null) ? 0 : pid.hashCode());
		result = prime * result + ((projectUser == null) ? 0 : projectUser.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Project other = (Project) obj;
		if (achievement == null) {
			if (other.achievement != null)
				return false;
		} else if (!achievement.equals(other.achievement))
			return false;
		if (approvalDate == null) {
			if (other.approvalDate != null)
				return false;
		} else if (!approvalDate.equals(other.approvalDate))
			return false;
		if (checkDate == null) {
			if (other.checkDate != null)
				return false;
		} else if (!checkDate.equals(other.checkDate))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (leader == null) {
			if (other.leader != null)
				return false;
		    } else if (!leader.equals(other.leader))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (pid == null) {
			if (other.pid != null)
				return false;
		} else if (!pid.equals(other.pid))
			return false;
		if (projectUser == null) {
			if (other.projectUser != null)
				return false;
		} else if (!projectUser.equals(other.projectUser))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Project [id=" + id + ", name=" + name + ", description=" + description + ", pid=" + pid + ", leader="
				+ leader + ", status=" + status + ", approvalDate=" + approvalDate + ", checkDate=" + checkDate
				+ ", achievement=" + achievement + ", projectUser=" + projectUser + "]";
	} 
}

User表的实体代码:

@Entity
@Table(name = "user", schema = "表名")
public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(length=36)
	private String id;
	@Column(length=45,nullable=false,unique=true)
    private String username;
	@Column(length=100,nullable=false,unique=true)
    private String email;
    
	@Column(length=45,nullable=false)
    private String password;
	@Column(length=45)
    private String role;
	@Column(columnDefinition="varchar(100) COMMENT '所属机构'")
    private String organization;
    
	@Column(columnDefinition="varchar(45) COMMENT '手机号'")
    private String phone;
    @OneToMany(mappedBy = "user",fetch=FetchType.EAGER)
	private List projectUser;


	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
    
    public User(User user){
        this.id = user.getId();
        this.username = user.getUsername();
        this.role = user.getRole();
        this.email = user.getEmail();
        this.password = user.getPassword();
        this.organization = user.getOrganization();
        this.phone = user.getPhone();
        this.projectUser = user.getProjectUser();
    }


	public String getId() {
		return id;
	}


	public String getUsername() {
		return username;
	}


	public String getEmail() {
		return email;
	}


	public String getPassword() {
		return password;
	}


	public String getRole() {
		return role;
	}


	public String getOrganization() {
		return organization;
	}


	public String getPhone() {
		return phone;
	}


	public List getProjectUser() {
		return projectUser;
	}


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


	public void setUsername(String username) {
		this.username = username;
	}


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


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


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


	public void setOrganization(String organization) {
		this.organization = organization;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}


	public void setProjectUser(List projectUser) {
		this.projectUser = projectUser;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((organization == null) ? 0 : organization.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		result = prime * result + ((projectUser == null) ? 0 : projectUser.hashCode());
		result = prime * result + ((role == null) ? 0 : role.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (organization == null) {
			if (other.organization != null)
				return false;
		} else if (!organization.equals(other.organization))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		if (projectUser == null) {
			if (other.projectUser != null)
				return false;
		} else if (!projectUser.equals(other.projectUser))
			return false;
		if (role == null) {
			if (other.role != null)
				return false;
		} else if (!role.equals(other.role))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
}

 

你可能感兴趣的:(Spring,SpringBoot,经验总结)