首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPA库房-多对一

JPA库房-多对一
EN

Stack Overflow用户
提问于 2021-01-30 20:44:22
回答 1查看 326关注 0票数 0

我有Student实体和Course实体。这是@ManyToOne关系,即Student一次只能参加一个课程,但课程可能有多个学生。

代码语言:javascript
复制
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String studentId;
    private String firstName;
    private String secondName;

    @ManyToOne
    @JoinColumn(name = "course_id")
    //@JsonIgnore
    private Course course;
@Entity
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String courseName;
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "course", orphanRemoval = true, targetEntity = Student.class)
private List<Student> students = new ArrayList<>();

我用以下json发布我的数据:

代码语言:javascript
复制
    {   
        "id": 1,
        "courseName": "course134",
        "students" : [
            {
                "id" : 1,
                "studentId": "123",
                "firstName": "John1",
                "secondName": "Name1"
                
            },
            {
                "id" : 2,
                "studentId": "1234567",
                "firstName": "John2",
                "secondName": "Name2"
                
            }

然后,当我得到课程时:

代码语言:javascript
复制
    {
        "id": 1,
        "courseName": "course134",
        "students": []
    }

如何列出参加特定课程的学生?我用StudentRepository编写了一个查询

代码语言:javascript
复制
    @Query("SELECT s from  Student s where s.id = :courseName")
        Optional<Student> getStudentByCourseName(String courseName);

还是不起作用。

这是我的存储库代码:

代码语言:javascript
复制
    @Repository
    public interface CourseRepository extends JpaRepository<Course, Long> {
        Optional<Course> findCourseByCourseName(String courseName);
        @Query("SELECT c.students FROM Course c WHERE c.courseName = :courseName")
        Optional<Student> getStudentsByCourseName(String courseName);
    }

这是我的服务方法

代码语言:javascript
复制
      public Optional<Student> findStudentByCourse(String courseName){
            return courseRepository.getStudentsByCourseName(courseName);
        }

最后我的主计长:

代码语言:javascript
复制
@GetMapping("/student/course/{courseName}")
public ResponseEntity<Student> findCoursesWithStudentId(@PathVariable String courseName) {
    Optional<Student> byCourseName = studentService.findStudentByCourse(courseName);
    if (byCourseName.isPresent()) {
        return ResponseEntity.ok(byCourseName.get());
    } else {
        return ResponseEntity.notFound().build();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-30 21:15:43

您应该查询课程表,而不是“学生”表。此外,查询将返回列表,而不仅仅是一个实体,因此更改方法的返回类型也是.

代码语言:javascript
复制
@Query("SELECT c.students FROM Course c WHERE c.courseName = :courseName")
List<Student> getStudentsByCourseName(String courseName) {}

编辑您可以这样做:

  1. 推出了简单的方法:

代码语言:javascript
复制
Course findByCourseName(String courseName) {}

  1. ,然后用一个简单的:

让它的学生

代码语言:javascript
复制
course.getStudents();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65972964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档