The select clause in Hibernate by R4R Team

The select clause picks which objects and properties to return in the query result set. The query will select mates of other Students.

Consider the following:

select mate

    from Student as student inner join student.mate as mate

 You can express this query more compactly as:

   select student.mate from Student student

Queries can return properties of any value type including properties of component type:

select cat.name from DomesticStudent  student where student.name like 'fri%'
select cust.name.firstName from Customer as cust

Queries can return multiple objects and/or properties as an array of type Object[]:

    select mother, offspr, mate.name from DomesticStudent as mother inner join mother.mate as mate
    left outer join mother.kittens as offspr
   
Or as a List:

    select new list(mother, offspr, mate.name) from DomesticCat as mother inner join mother.mate as         mate left outer join mother.kittens as offspr

Or - assuming that the class Family has an appropriate constructor - as an actual typesafe Java object:

    select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate
    left join mother.kittens as offspr

 can assign aliases to selected expressions using as:

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n from Cat cat

This is most useful when used together with select new map. This query returns a Map from aliases to selected values:

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Student student

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!