Java 在HQL中使用单个select语句获取数据(一对多关系)

Java 在HQL中使用单个select语句获取数据(一对多关系),java,hibernate,orm,Java,Hibernate,Orm,我有两个实体:学生和大学。一所大学有多个学生 @Entity public class College { @Id @GeneratedValue private int collegeId; private String collegeName; public int getCollegeId() { return collegeId; } public void setCollegeId(int collegeI

我有两个实体:学生和大学。一所大学有多个学生

@Entity
public class College {

    @Id
    @GeneratedValue
    private int collegeId;

    private String collegeName;

    public int getCollegeId() {
        return collegeId;
    }

    public void setCollegeId(int collegeId) {
        this.collegeId = collegeId;
    }

    public String getCollegeName() {
        return collegeName;
    }

    public void setCollegeName(String collegeName) {
        this.collegeName = collegeName;
    }
}


@Entity
public class Student {

    @Id
    @GeneratedValue
    private int studentId;

    private String studentName;

    @ManyToOne(cascade = CascadeType.ALL)
    private College college;

    public int getStudentId() {
        return studentId;
    }

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

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public College getCollege() {
        return college;
    }

    public void setCollege(College college) {
        this.college = college;
    }
}
现在我想选择say collegeId=1的所有学生。我可以通过使用本机SQL的单选查询实现这一点:

Select * from student where collegeid = 1
我无法使用HQL实现单选择查询。可能吗?如果是,那怎么办

实用程序类别:

public class ManyToOne {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.hibernate.examples");

        EntityManager em = emf.createEntityManager();

        College college1 = new College();
        college1.setCollegeName("College1");


        College college2 = new College();
        college2.setCollegeName("College2");

        Student student1 = new Student();
        student1.setStudentName("std1");
        student1.setCollege(college1);


        Student student2 = new Student();
        student2.setStudentName("std2");
        student2.setCollege(college2);

        Student student3 = new Student();
        student3.setStudentName("std3");
        student3.setCollege(college1);

        Student student4 = new Student();
        student4.setStudentName("std4");
        student4.setCollege(college1);

        em.getTransaction().begin();

        em.persist(college1);
        em.persist(college2);
        em.persist(student1);
        em.persist(student2);
        em.persist(student3);
        em.persist(student4);

        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();
        em.getTransaction().begin();

        String queryString = "select student from "+Student.class.getName()+" student where student.college.collegeId = 1";

        Query query = em.createQuery(queryString);

        List<Student> students = query.getResultList();

        em.getTransaction().commit();
        em.close();
        emf.close();

    }
}
HQL

Select * from Student s where s.college.collegeId = 1

我已经试过了。然而,在学生上执行select之后,hibernate正在学院表上执行额外的查询。不知道为什么。My HQL:
从“+student.class.getName()+”student where student.college.collegeId=1”中选择student
hibernate执行的查询是什么?您可以通过在hibernate配置中打开show_sql=true来查看它们。我已经添加了相关的生成输出查询。仅第一个查询就足够了,它在我的数据库中也执行得很好。不确定生成第二个查询的原因。通过将FetchType of ManyTone mapping另外设置为LAZY(默认情况下是“渴望”)解决了附加查询的问题。
Select * from Student s where s.college.collegeId = 1