java - 为什么在多对一关系中获取数据时要执行额外的查询?

标签 java hibernate orm

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

@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;
    }
}

我想获取特定学院的所有学生。

正如您在下面的代码中看到的,我编写的 HQL 查询是:“从“+Student.class.getName()+”学生中选择学生,其中 Student.college.collegeId = 1”

执行以下代码时,会触发两个 SQL 查询,如下所示:

Hibernate: select student0_.studentId as studentId1_1_, student0_.college_collegeId as college_collegeId3_1_, student0_.studentName as studentName2_1_ from mevada.Student student0_ where student0_.college_collegeId=1
Hibernate: select college0_.collegeId as collegeId1_0_0_, college0_.collegeName as collegeName2_0_0_ from mevada.College college0_ where college0_.collegeId=?

理想情况下,第一个查询足以获取所有所需的学生,并且当我直接从数据库触发时,它运行良好。

为什么执行第二个查询?如何通过执行这个额外的查询来停止 Hibernate? 实用程序类:

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();

    }
}

最佳答案

就是因为这个

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

@ManyToOne 默认情况下是 EAGER,这意味着从数据库获取 Student 时会填充它。您可以将关系设置为LAZY,这会延迟第二个查询,直到您调用student.getCollege()。但是,如果您知道自己还需要大学数据,那么您应该能够通过像这样的一个查询来获取所有数据

"select student from " + Student.class.getName() + " student inner join student.college col where col.collegeId = 1"

关于java - 为什么在多对一关系中获取数据时要执行额外的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27507879/

相关文章:

php - 如何对多个字段进行分组,包括嵌套的急切列

mysql - Sequelize 查询显示多对多关系中的附加数据

java - 如何使用 JRPrintServiceExporter 更改边距而不显示打印对话框?

java - 在 Eclipse 中使用 Hibernate 工具对 Oracle 数据库进行逆向工程

java - 从 Eclipse 插件中的文件读取

java - tomcat Web 应用程序上的表情符号问题

java - hibernate 环境 : Initializing Envers Proxies

database-design - OO 数据模型 - 我可以忽略对象关系映射器约束吗?

java - 将 Java 应用程序从 Windows 移植到 Linux - 字符格式

java - 在 Apache Spark (Java) 中按多个值对 JavaRDD 元组进行排序