my dog learned polymorphism*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes JPQL Query question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "JPQL Query question" Watch "JPQL Query question" New topic

JPQL Query question

Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
This is a drag-and-drop question from the trial Enthuware kit:

there are 2 entities:

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

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

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é

Joined: Nov 24, 2005
Posts: 14687

Check the spec again, Fetch Joins. It is written that the Fetch Joins notation is :
fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH join_association_path_expression

[My Blog]
All roads lead to JavaRanch
Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
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

Joined: Jul 06, 2008
Posts: 85
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...

SCDJWS 5.0, SCBCD 5.0, SCWCD 5.0, SCJP 5.0
I agree. Here's the link:
subject: JPQL Query question
Similar Threads
Conditional (Ternary) Operator (?:)
Unidirectional manytomany not persisting
Using ID of link as Parameter
Another Enthuware question doubt regarding JPQL
StringBuffer / StringBuilder methods