67,512
社区成员
发帖
与我相关
我的任务
分享
package com.eseasky.spring.springboot.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name = "course")
public class Course {
private String pid;
private String courseName;// 课程名称
private Set<Student> Student = new HashSet<Student>(); // 选修课程学生
public Course() {
}
public Course(String pid, String courseName, Set<Student> student) {
this.pid = pid;
this.courseName = courseName;
this.Student = student;
}
@Id
@Column(name = "pid", unique = true, nullable = false, length = 32)
@GeneratedValue(generator = "generator")
@GenericGenerator(name = "generator", strategy = "uuid")
public String getPid() {
return pid;
}
@Column(name = "course_name", unique = true, length = 64)
public String getCourseName() {
return courseName;
}
//mappedBy :
// 表示当前所在表和 Student 的关系是定义在 Student 里面的 course 这个成员上面的,
// 他表示此表是一对一关系中的从表,也就是关系是在 Student 表中维护的,
// Student 表是关系的维护者,有主导权,它有个外键指向 course (Student 中的 getCourse() )
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "course",cascade={CascadeType.MERGE})
//NotFound : 意思是找不到引用的外键数据时忽略,NotFound默认是exception
@NotFound(action = NotFoundAction.IGNORE)
public Set<Student> getStudent() {
return Student;
}
public void setPid(String pid) {
this.pid = pid;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setStudent(Set<Student> student) {
Student = student;
}
}
package com.eseasky.spring.springboot.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "student")
public class Student {
private String pid;
private String studentName;
private Set<Course> course = new HashSet<Course>(); //选修课程
public Student() {
}
public Student(String pid, String studentName, Set<Course> course) {
this.pid = pid;
this.studentName = studentName;
this.course = course;
}
@Id
@Column(name = "pid", nullable = false, length = 32)
@GeneratedValue(strategy=GenerationType.IDENTITY)
public String getPid() {
return pid;
}
@Column(name = "student_name", unique = true, length = 64)
public String getStudentName() {
return studentName;
}
/**
* Hibernate 会自动创建一张关系表stu_cou, 里边有俩字段stu_id和cou_id分别为两表主键
*
* @return
*/
@ManyToMany(cascade = {CascadeType.MERGE,CascadeType.PERSIST},fetch=FetchType.EAGER)
@JoinTable(name = "stu_cou",
joinColumns = {@JoinColumn(name = "stu_id")},
inverseJoinColumns = {@JoinColumn(name = "cou_id")})
public Set<Course> getCourse() {
return course;
}
public void setPid(String pid) {
this.pid = pid;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setCourse(Set<Course> course) {
this.course = course;
}
}
package com.eseasky.spring.springboot.repository.business;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.eseasky.spring.springboot.models.Course;
@Repository
public interface CourseRepository extends JpaRepository<Course, Long> {
Course findByCourseName(String name);
Course findByPid(String id);
}
package com.eseasky.spring.springboot.repository.business;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.eseasky.spring.springboot.models.Student;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByStudentName(String name);
Student findByPid(String id);
}
package com.eseasky.spring.springboot.services.impl;
import java.util.Set;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eseasky.spring.springboot.models.Course;
import com.eseasky.spring.springboot.models.Student;
import com.eseasky.spring.springboot.repository.business.CourseRepository;
import com.eseasky.spring.springboot.repository.business.StudentRepository;
import com.eseasky.spring.springboot.services.ManyToMany;
@Transactional(rollbackOn=Exception.class)
@Service
public class ManyToManyImpl implements ManyToMany{
@Autowired
CourseRepository courseRepository;
@Autowired
StudentRepository studentRepository;
/**
* 查学生
* @param sId
* @return
*/
public Student findBySPid(String sId) {
return studentRepository.findByPid(sId);
}
/**
* 查课程
* @param cId
* @return
*/
public Course findByCPid(String cId) {
return courseRepository.findByPid(cId);
}
/**
* 选修课程 学生编号 课程编号
* @param sId
* @param cId
*/
public void slectingCourse(String sId,String cId) {
Student student = findBySPid(sId);
Set<Course> courses = student.getCourse();
Course course = findByCPid(cId);
courses.add(course);
student.setCourse(courses);
studentRepository.save(student);
}
public void delCourse(String sId,String cId) {
Student student = findBySPid(sId);
Set<Course> courses = student.getCourse();
Course course = findByCPid(cId);
courses.remove(course);
}
}