如何在休眠状态下使用条件查询获取详细信息?

时间:2018-12-19 12:45:05

标签: hibernate jpa hql criteria hibernate-criteria

我们有两个表学生标记。我们可以将这些表与studentid连接起来。 (在普通选择查询中,从学生st中选择*,标记mk,其中st.studentId = mk.studentId) 能帮我如何创建条件查询以获取详细信息吗?

学生的实体类如下:-

 @Entity
    @Table(name = "student")
    public class Student{
    private int studentId;
    private String name;

      @Column(name = "studentId")
      public int getStudentId() {
        return studentId;
      }

      public void setStudentId(String studentId) {
        this.studentId = studentId;
      }

      @Column(name = "name")
      public String getName() {
        return name;
      }

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

Mark的实体类如下:-

@Entity
@Table(name = "mark")
public class Mark{
private int markId;
private int studentId;
private String name;
private String mark

  @Column(name = "markId")
  public int getMarkId() {
    return markId;
  }

  public void setMarkId(String markId) {
    this.markId = markId;
  }

  @Column(name = "studentId")
  public int getStudentId() {
    return studentId;
  }

  public void setStudentId(String studentId) {
    this.studentId = studentId;
  }

  @Column(name = "markId")
  public int getMarkId() {
    return markId;
  }

  public void setMarkId(String markId) {
    this.markId = markId;
  }

  @Column(name = "name")
  public String getName() {
    return name;
  }

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

 @Column(name = "mark")
  public String getMark() {
    return mark;
  }

  public void setMark(String mark) {
    this.mark = mark;
  }
}

1 个答案:

答案 0 :(得分:0)

Hibernate是一个 Object 关系映射工具。您可以在Java代码中映射对象之间的关系。

    @Entity
    @Table(name = "student")
    public class Student{
    private int studentId;
    private String name;
    private Set<Mark> marks; 

      @Column(name = "studentId")
      public int getStudentId() {
        return studentId;
      }

      public void setStudentId(String studentId) {
        this.studentId = studentId;
      }

      @Column(name = "name")
      public String getName() {
        return name;
      }

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

      @OneToMany(mappedBy = "student")
      public Set<Student> getStudents(){
        return students;
      }
    } 

标记:

@Entity
@Table(name = "mark")
public class Mark{
private int markId;
private Student student;
private String name;
private String mark

  @Column(name = "markId")
  public int getMarkId() {
    return markId;
  }

  public void setMarkId(String markId) {
    this.markId = markId;
  }

  @ManyToOne
  @JoinColumn(name = "studentId")
  public Student getStudent() {
    return student;
  }

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

  @Column(name = "markId")
  public int getMarkId() {
    return markId;
  }

  public void setMarkId(String markId) {
    this.markId = markId;
  }

  @Column(name = "name")
  public String getName() {
    return name;
  }

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

 @Column(name = "mark")
  public String getMark() {
    return mark;
  }

  public void setMark(String mark) {
    this.mark = mark;
  }
}