Вы находитесь на странице: 1из 18

HQL

Aplicaciones Distribuidas
http://docs.jboss.org/hibernate/core/3.5/reference/esES/html/queryhql.html

Hibernate utiliza un lenguaje de consulta potente


(HQL) que se parece a SQL.

Sin embargo, comparado con SQL, HQL es


completamente orientado a objetos y
comprende nociones como herencia,
polimorfismo y asociacin.

Las consultas no son sensibles a maysculas, a


excepcin de los nombres de las clases y
propiedades Java.
SeLeCT es lo mismo que sELEct e igual a SELECT
org.hibernate.eg.FOO no es lo mismo que
org.hibernate.eg.Foo

from
from eg.Cat
from Cat
(auto-import es el valor predeterminado)

from Cat as cat


(referirse al Cat en otras partes de la peticin)

from Formula as form, Parameter as param


(los alias de consulta utilizando una inicial en minsculas, p.e.: domesticCat)

Asociaciones y uniones (joins)


inner join
left outer join
right outer join
from Cat as cat inner join cat.mate as mate left outer
join cat.kittens as kitten
from Cat as cat left join cat.mate.kittens as kittens
(abreviados de joins)
5

HQL soporta dos formas de unin de asociacin:


implicit y explicit.
from Cat as cat join cat.mate as mate where
mate.name like '%s%
from Cat as cat where cat.mate.name like '%s%
(la forma implicit no utiliza la palabra clave join)
(las asociaciones se "desreferencian" utilizando la
notacin punto.)

select

La clusula select escoge qu objetos y propiedades


devolver en el conjunto de resultados de la consulta
select mate from Cat as cat inner join cat.mate as
mate

select cat.mate from Cat cat


7

Funciones de agregacin
avg(...), sum(...), min(...), max(...)
count(*), count(...), count(distinct ...), count(all...)
select avg(cat.weight), sum(cat.weight), max(cat.weight),
count(cat) from Cat cat
select cat.weight + sum(kitten.weight) from Cat cat
cat.kittens kitten group by cat.id, cat.weight

join

select count(distinct cat.name), count(cat) from Cat cat


(con distinct)
9

Consultas polimrficas

from Cat as cat


(devuelve instancias no slamente de Cat, sino tambin
de subclases como DomesticCat)
(cualquier clase o interfaz Java en la clusula from)

from java.lang.Object o
(retornara todos los objetos persistentes)
10

11

Las expresiones utilizadas en la clusula where incluyen lo siguiente:


operadores matemticos: +, -, *, /
operadores de comparacin binarios: =, >=, <=, <>, !=, like
operadores lgicos and, or, not
Parntesis ( ) que indican agrupacin
in, not in, between, is null, is not null, is empty, is not empty, member of y not
member of
Caso "simple", case ... when ... then ... else ... end, y caso "buscado", case when ...
then ... else ... end
concatenacin de cadenas ...||... o concat(...,...)
current_date(), current_time() y current_timestamp()
second(...), minute(...), hour(...), day(...), month(...), and year(...)

12

from DomesticCat cat where cat.name between 'A' and 'B'

from DomesticCat cat where cat.name not in ( 'Foo', 'Bar',


'Baz' )

select case f.type when 'REMOVE' then f.previousLocation


else f.currentLocation end from FileOperation f

13

HQL:

select cust
from Product prod,
Store store
SQL:
inner join store.customers cust
where prod.name = 'widget'
SELECT cust.name, cust.address,
cust.phone, cust.id, cust.current_order and store.location.name in ( 'Melbourne',
FROM customers cust,
'Sydney' )
stores store,
and prod = all
locations loc,
elements(cust.currentOrder.lineItems)
store_customers sc,
product prod
WHERE prod.name = 'widget'
AND store.loc_id = loc.id
AND loc.name IN ( 'Melbourne', 'Sydney' )
AND sc.store_id = store.id
AND sc.cust_id = cust.id
AND prod.id = ALL(
SELECT item.prod_id
FROM line_items item, orders o
WHERE item.order_id = o.id
AND cust.current_order = o.id )
14

order by
from DomesticCat cat order by cat.name asc, cat.weight desc,

cat.birthdate

group by
select foo.id, avg(name), max(name) from Foo foo join foo.names

name group by foo.id


select cat.color, sum(cat.weight), count(cat) from Cat cat group by

cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)


(where de group by)

15

16

Ejemplo

17

18

Вам также может понравиться