• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPQL Query question

 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a drag-and-drop question from the trial Enthuware kit:


there are 2 entities:

Entity 1:
<blockquote>code:
<pre name="code" class="core">
@Entity public class Presentation {
...
@ManyToOne
private Student presenter;
...
}
</pre>
</blockquote>

Entity 2:
<blockquote>code:
<pre name="code" class="core">
@Entity public class Student {
...
private int score;
@OneToMany(cascade=CascadeType.ALL, fetch=LAZY, mappedBy="presenter")
private Collection<Presentation> presentation;
...
}
</pre>
</blockquote>

Design a query that will retrieve all the students along with their associated presentations.

SELECT s from Student s ________ ________ _________ _________

The options given are: EAGER, FETCH, JOIN, s.presentations, LEFT, INNER, presentations, Presentation

The answer given is: SELECT s from Student s LEFT JOIN FETCH s.presentations

My doubt is that how can LEFT, FETCH and JOIN come together? In the spec, it is mentioned that there can be LEFT JOIN and FETCH JOIN seperately. Also, why should we need FETCH here when we want to retrieve all students and all their associated presentations...???

please help.
waiting eagerly for a reply...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the spec again, 4.4.5.3 Fetch Joins. It is written that the Fetch Joins notation is :
fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH join_association_path_expression
 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, its my mistake.

Although said that, i still haven't understood the proper way on how to use the LEFT OUTER JOIN AND FETCH JOIN clauses. the spec isnt clear enough. Could you please expalin with an example...
 
Vinay Nath
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Aditya,

INNER JOIN - you get everythin in A for which there is something in B, you write it as A INNER JOIN B or A JOIN B, INNER is optional.

OUTER JOIN - you get everything in A even for which there is nothing B, wriiten as A LEFT OUTER JOIN B or A LEFT JOIN B, outer is optional, these concepts are same as in SQL, the next part however is smthin new:

FETCH JOIN - You can make a left (outer) join as a fetch join by specifying it in query. It specified as just FETCH before a LEFT (OUTER) JOIN. This join is used to get data from an entity which has tht same data annotated as fetch=lazy, so to eagerly fetch it for use in query we use fetch join.

Remember it is not safe according to spec to access data tht has been marked as lazy fetch unless you access it before using it or get it in query using fetch join.

That question in enthuwarte was testing two points mainly: 1) knowlwdge of joins 2) wheether you specify FETCH before or after JOIN. hope this helps...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic