Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware doubt on JPQL

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have the following doubts from enthuware.

1)

Consider the following entities -

@Entity public class Student{
...
String firstname;
@OneToMany
Collection<Presentation> presentations;
...
}

@Entity public class Presentation{
...
}

Which of the following JPQL queries will return the firstname and the number of presentations that a student has made?


Please select 1 option

a)select s.firstname, count(s.presentations) from Student s
b)select s.firstname, count(s.presentations) from Student s group by s.firstname
c)select s.firstname, count(s.presentations) from Student s group by s.presentations
d)select s.firstname, count(p) from Student s left join s.presentations p

The right answer for this one is option b.


My doubt is how can we select count(s.presentations) directly as specified in option b.

Because the spec says "it is illegal to select elements directly from a collection-based relationship."


Thanks in advance

With Regards
Deepthi


 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just quessing..but I think that the spec means that you musn't try to do something like:


 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute I'm wrong...what you said is correct..you can't access an element in a collection at all in a JPQL query...But in your example the collection is not actually being accessed...an aggregate function(I think count is an aggregate function) is just being applied to the collection as a whole.So the correct answer is b.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic