【问题标题】:Fetch data using single select statement in HQL (One to many relationsheep)使用 HQL 中的单个选择语句获取数据(一对多关系)
【发布时间】:2014-12-16 18:25:51
【问题描述】:

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

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

现在我想选择所有大学 ID = 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();

    }
}

生成的查询:

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=?

【问题讨论】:

    标签: java hibernate orm


    【解决方案1】:

    HQL

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

    【讨论】:

    • 我已经尝试过了。然而,在学生休眠中执行选择后,正在对大学表执行附加查询。无法弄清楚为什么。我的 HQL:select student from "+Student.class.getName()+" student where student.college.collegeId = 1
    • hibernate执行了哪些查询?您可以通过在休眠配置中打开 show_sql=true 来查看它们
    • 我添加了有问题的生成输出查询。仅第一个查询就足够了,它在我的数据库中也运行良好。不确定为什么会生成第二个查询。
    • 附加查询的问题已通过将 ManyToOne 映射的 FetchType 设置为 LAZY 来解决,默认情况下它是 EAGER。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多