springboot框架中@manytomany的问题

m0_37502844 2017-09-16 11:50:59
在做@manytomany多关系映射的时候,查出来的数据是两张表的内容相互嵌套的结果

不知道这个问题怎么解决

设置了两个类,学生和课程类,学生和课程构成多对多的关系
下面是两个类的结构
Course类
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;
}

}

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

}


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


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

调用slectingCourse函数增加学生选课关系成功
中间关系表stu_cou中插入了两条数据
但是调用findBySPid查学生信息的时候 查出来的信息是两个类相互嵌套了的


不知道这个问题怎么解决

...全文
263 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
m0_37502844 2017-09-19
  • 打赏
  • 举报
回复
问题解决了 在循环嵌套之后 后台日志会打印错误 spring-jpa-mysql-could-not-write-content-infinite-recursion-stackoverflowerro 在@manytomany的关联字段上面加上 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "cid") 这个是解决返回的json嵌套的问题的

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧