基于集合的最大值对实体进行排序

时间:2016-09-19 09:42:22

标签: entity-framework hibernate sorting jpa one-to-many

我有一个看起来像这样的实体:

  • 学生 - 实体

  • 主题 - 实体

学生可以有很多科目,所以这是一对多的关系。 我需要在JPA中编写以下查询[named / typed query](但不是原生的)

获得具有最高分数的前n名(页面大小)学生,无论主题如何。 任何科目中的最高分。并且必须退回一次学生记录。

  • 页面大小 - 我已经使用了firstResult()和maxResults()来实现它的工作
  • 排序 - 需要找到解决方案

测试数据: Data

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Student {

    @Id
    private Integer studentId;

    private String name;

    @OneToMany
    private List<Subject> subjects;

    public Integer getStudentId() {
        return studentId;
    }

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

    public String getName() {
        return name;
    }

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

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

}

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Subject {
    @Id
    private Integer subjectId;

    private String subjectName;

    private Integer marks;

    public Integer getSubjectId() {
        return subjectId;
    }

    public void setSubjectId(Integer subjectId) {
        this.subjectId = subjectId;
    }

    public Integer getMarks() {
        return marks;
    }

    public void setMarks(Integer marks) {
        this.marks = marks;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

}

1 个答案:

答案 0 :(得分:0)

以下程序将为您提供排序所需的输出。

数据检索类

public static void main(String[] args) {
    SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
    Session session = sessionFactory.openSession();

    Criteria c = session.createCriteria(Student.class, "student");
    c.setFetchMode("student.subjects", FetchMode.JOIN);
    c.createAlias("student.subjects", "subjects");

    ProjectionList columns = Projections.projectionList()
            .add(Projections.property("student.name"))
            .add(Projections.property("subjects.subjectName"))
            .add(Projections.property("subjects.marks"));
    c.setProjection(columns);
    c.addOrder(Order.desc("subjects.marks"));

    List<Object[]> list = c.list();
    for (Object[] arr : list) {
        System.out.println(Arrays.toString(arr));
    }

    System.out.println("Data is retrieved successfully...");
}

学生POJO课程

package com.sorting;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

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

@Id
private Integer studentId;

private String name;

@OneToMany(cascade = CascadeType.ALL)
private List<Subject> subjects;

public Integer getStudentId() {
    return studentId;
}

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

public String getName() {
    return name;
}

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

public List<Subject> getSubjects() {
    return subjects;
}

public void setSubjects(List<Subject> subjects) {
    this.subjects = subjects;
}

@Override
public String toString() {
    return "Student [studentId=" + studentId + ", name=" + name
            + ", subjects=" + subjects + "]";
}
}

主题POJO课程

package com.sorting;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Subject {
@Id
private Integer subjectId;
private String subjectName;
private Integer marks;

public Integer getSubjectId() {
    return subjectId;
}

public void setSubjectId(Integer subjectId) {
    this.subjectId = subjectId;
}

public Integer getMarks() {
    return marks;
}

public void setMarks(Integer marks) {
    this.marks = marks;
}

public String getSubjectName() {
    return subjectName;
}

public void setSubjectName(String subjectName) {
    this.subjectName = subjectName;
}

@Override
public String toString() {
    return "Subject [subjectId=" + subjectId + ", subjectName="
            + subjectName + ", marks=" + marks + "]";
}
}

hibernate.cfg.xml 映射

    <mapping class="com.sorting.Student"/>
    <mapping class="com.sorting.Subject"/>

数据保存类

public static void main(String[] args) {
    ArrayList<Subject> s1SubjectList = new ArrayList<Subject>();//Student 1 Subject List
    ArrayList<Subject> s2SubjectList = new ArrayList<Subject>();//Student 2 Subject List
    ArrayList<Subject> s3SubjetcList = new ArrayList<Subject>();//Student 3 Subject List

    Subject s1s1= new Subject();//Student 1 Subject 1
    s1s1.setSubjectId(1);
    s1s1.setSubjectName("ENG");
    s1s1.setMarks(85);
    //Add to List
    s1SubjectList.add(s1s1);
    Subject s1s2= new Subject();//Student 1 Subject 2
    s1s2.setSubjectId(2);
    s1s2.setSubjectName("MATHS");
    s1s2.setMarks(89);
    //Add to List
    s1SubjectList.add(s1s2);
    Subject s1s3= new Subject();//Student 1 Subject 3
    s1s3.setSubjectId(3);
    s1s3.setSubjectName("PHY");
    s1s3.setMarks(90);
    //Add to List
    s1SubjectList.add(s1s3);

    Subject s2s1= new Subject();//Student 2 Subject 1
    s2s1.setSubjectId(4);
    s2s1.setSubjectName("ENG");
    s2s1.setMarks(90);
    //Add to List
    s2SubjectList.add(s2s1);
    Subject s2s2= new Subject();//Student 2 Subject 2
    s2s2.setSubjectId(5);
    s2s2.setSubjectName("MATHS");
    s2s2.setMarks(95);
    //Add to List
    s2SubjectList.add(s2s2);
    Subject s2s3= new Subject();//Student 2 Subject 3
    s2s3.setSubjectId(6);
    s2s3.setSubjectName("PHY");
    s2s3.setMarks(89);
    //Add to List
    s2SubjectList.add(s2s3);

    Subject s3s1= new Subject();//Student 3 Subject 1
    s3s1.setSubjectId(7);
    s3s1.setSubjectName("ENG");
    s3s1.setMarks(96);
    //Add to List
    s3SubjetcList.add(s3s1);
    Subject s3s2= new Subject();//Student 3 Subject 2
    s3s2.setSubjectId(8);
    s3s2.setSubjectName("MATHS");
    s3s2.setMarks(85);
    //Add to List
    s3SubjetcList.add(s3s2);
    Subject s3s3= new Subject();//Student 3 Subject 3
    s3s3.setSubjectId(9);
    s3s3.setSubjectName("PHY");
    s3s3.setMarks(99);
    //Add to List
    s3SubjetcList.add(s3s3);

    //Create Student 1
    Student s1=new Student();
    s1.setStudentId(1);
    s1.setName("Foo");
    s1.setSubjects(s1SubjectList);

    //Create Student 2
    Student s2=new Student();
    s2.setStudentId(2);
    s2.setName("Bar");
    s2.setSubjects(s2SubjectList);

    //Create Student 3
    Student s3=new Student();
    s3.setStudentId(3);
    s3.setName("Doo");
    s3.setSubjects(s3SubjetcList);

    SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.persist(s1);
    session.persist(s2);
    session.persist(s3);
    tx.commit();
    session.close();
    System.out.println("Student entries saved...");
}

输出:输出根据特定科目中学生的最高分数进行排序。

Output