简体   繁体   English

如何编写这个JPA查询字词?

[英]How to write this JPA query term?

I have two tables; 我有两个桌子。

Create table Student (
    id int NOT NULL AUTO_INCREMENT,
    name varchar(35),
    PRIMARY KEY (id)
);

Create table Course (
    id int NOT NULL AUTO_INCREMENT,
    student_id int,
    name varchar(35),
    PRIMARY KEY (id),
    FOREIGN KEY (student_id)
    REFERENCES Student(id)
    ON DELETE CASCADE
);

Query: find all course taken by the student "John". 查询:查找学生“约翰”所修的所有课程。

Select C.name 
FROM Course C
JOIN Student S
  ON S.id=C.student_id 
WHERE S.name='John';

How to write this in JPA query langauge? 如何用JPA查询语言编写此代码? How to write criteria involving two tables like this which are related by a foreign key? 如何编写涉及两个这样的与外键相关的表的条件?

public List<Course> findCourseByStudentName(String name) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Course> criteria = cb.createQuery(Course.class);
    Root<Course> courseRoot = criteria.from(Course.class);

    Join<Course,Student> courseStudent = courseRoot.join(Course_.student);
    criteria.select(courseRoot).where(cb.equal(courseStudent.get(Student_.name), "John"));

}

Edited: This is the Student class, Student.java 编辑:这是学生类Student.java

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Student implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 35)
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy="student")
    Set<Course> courses;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public void setCourses(Set<course> courses) {
        this.course = course;
    }        

    public Set<Course> getCourses() { 
       return courses; 
    }
}

This is the Course class, Course.java 这是课程类Course.java

@Entity
@Table
public class Course implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull 
    @ManyToOne( targetEntity = Student.class ) 
    @JoinColumn( name = "student_id", referencedColumnName = "id") 
    private Student student; 

    @NotNull
    @Size(min = 1, max = 35)
    private String name;

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

    public Long getId() {
        return id;
    }

    public void setStudent(String student) {
        this.student = student;
    }

    public String getStudent() {
        return student;
    }

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

    public String getName() {
        return Name;
    }
}

First of all, you must create two entities Student and Course and add at least @ManyToOne relationship in Course . 首先,您必须创建两个实体StudentCourse并在Course中至少添加@ManyToOne关系。 Than you can use JPQL, something ike this: 比起JPQL,您可以使用以下功能:

    SELECT C.name
      FROM Cource C
INNER JOIN Student S
     WHERE S.name = :studentName

After that you should add parameter in query and get List<String> as result 之后,您应该在查询中添加参数并获得List<String>作为结果

My latest edition gave the correct answer. 我的最新版本给出了正确的答案。 Thanks for all comments. 感谢所有评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM