• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPQL Query question

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic