0

I have two entities student and college. A single college has multiple student.

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

I want to fetch all the students of a particular college.

As you can see in below code the HQL query I have written is : "select student from "+Student.class.getName()+" student where student.college.collegeId = 1"

On execution of following code I two SQL queries are fired as follows:

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

Ideally first query is sufficient to fetch all required students and it is working well when I fire directly from database.

why second query is executed? How can I stop Hibernate by executing this extra query? Utility class:

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

    }
}
vatsal mevada
  • 3,808
  • 5
  • 28
  • 57

1 Answers1

2

It's because of this

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

@ManyToOne is EAGER by default, which means it is populated when Student is fetched from database. You could set the relation to be LAZY, that would delay that second query until you call student.getCollege(). But if you know you'll need college data as well, you should be able to get it all in one query like this

"select student from " + Student.class.getName() + " student inner join student.college col where col.collegeId = 1"
Predrag Maric
  • 21,996
  • 4
  • 45
  • 64
  • I changed FetchType to LAZY and I am facing this exception: java.lang.ClassCastException: entities.College_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy – vatsal mevada Dec 17 '14 at 06:20
  • 1
    Check out [this thread](http://stackoverflow.com/questions/22481540/hibernate-exception-javassist-0-cannot-be-cast-to-javassist-util-proxy-proxy) for possible solutions. Common reason seems to be some dependency issues. – Predrag Maric Dec 17 '14 at 09:49
  • Thanks @Predrag. I have already fixed the exception from the same thread. Didn't got time to mention the same. Thanks. – vatsal mevada Dec 17 '14 at 13:28