Reputation: 2238
I am a newbie to Hibernate and while going through the fetching associations in joins I came across the below query:
em.createQuery(select guide from Guide guide join fetch guide.Students students);
The above query in sql looks like :
select
guide0_id as id_1_2_0,
students1_id as id_1_6_1,
guide0_name as name_2_2_0,
students1_guide_id as guide_id_4_6_1....
Student and guide have a manytoone relationship with Student being the owner of the relationship. The above query will fetch students details as well even though guide is lazily loaded by default. My question is if our intention is to have students in the select clause as well as seen in the SQL above, why can't we do a simple:
em.createQuery(select guide,students from Guide guide join guide.Students
students);
By adding students
column in the select
clause?
Upvotes: 1
Views: 295
Reputation: 4224
The above query will fetch students details as well even though guide is lazily loaded by default
Because you have used 'join fetch' instead of just 'join', you get students loaded eagerly.
Upvotes: 2
Reputation: 77
// you can use simple join query to fetch your desired record as you want
// and also you can get by Hibernate Criteria instead of using the SQL Query like
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="child_table_column",referencedColumnName="id")
private ParentTable updatedBy;
// use above for POJO Configuration and then use simple criteria to get the DATa
Upvotes: 0