获取其他实体列表

时间:2017-09-29 06:04:31

标签: java json spring-data-jpa

我正在使用sprint jpa数据来获取学生及其相应课程的列表。

  @Entity
@Table(name = "student")
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "student_id")
    private Integer studentId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "fname")
    private String fname;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "lname")
    private String lname;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "password")
    private String password;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    @JsonManagedReference
    private Set<StudentCourse> studentCourseSet;

课程实体

@Entity
@Table(name = "course")
public class Course implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "course_id")
    private Integer courseId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "couse_code")
    private String couseCode;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "course_name")
    private String courseName;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
    @JsonManagedReference
    private Set<StudentCourse> studentCourseSet;

学生课程

@Entity
@Table(name = "student_course")
public class StudentCourse implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected StudentCoursePK studentCoursePK;
    @JoinColumn(name = "course_id", referencedColumnName = "course_id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    @JsonBackReference
    private Course course;
    @JoinColumn(name = "student_id", referencedColumnName = "student_id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    @JsonBackReference
    private Student student;

并且

@Embeddable
public class StudentCoursePK implements Serializable {

    @Basic(optional = false)
    @NotNull
    @Column(name = "student_id")
    private int studentId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "course_id")
    private int courseId;

我有这个RestController返回学生列表。

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @Autowired
    StudentRepositoryImpl studentRepositoryCustomImpl;

    @RequestMapping(value = "/studentlist")
    public Iterable<Student> getStudentList() {
        return studentService.getStudentList();
    }

输出如下

// 20170929084542 // http://localhost:8080/studentlist

[
  {
    "studentId": 1,
    "fname": "abc",
    "lname": "efg",
    "email": "a@b.com",
    "username": "as",
    "password": "as",
    "studentCourseSet": [
      {
        "studentCoursePK": {
          "studentId": 1,
          "courseId": 4
        }
      },
      {
        "studentCoursePK": {
          "studentId": 1,
          "courseId": 1
        }
      },
      {
        "studentCoursePK": {
          "studentId": 1,
          "courseId": 2
        }
      },
      {
        "studentCoursePK": {
          "studentId": 1,
          "courseId": 3
        }
      }
    ]
  }
]

如何在输出中包含学生的学生课程名单。我正在使用春季靴子

0 个答案:

没有答案