http://www.hibernate.org/hib_docs/refer ... ryhql.html
HQL (it's quite a complex HQL query)
select cust
from Product prod,
Store store
inner join store.customers cust
where prod.name = 'widget'
and store.location.name in ( 'Melbourne', 'Sydney' )
and prod = all elements(cust.currentOrder.lineItems)
in sql it will be something like:
SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order
FROM customers cust,
stores store,
locations loc,
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
)
this was to demonstrate HQL power.
Now let's talk about ORM, you must realize what Object Relationnal Mapping is.
It allows you to work with a fine grained domain model.
It may contain one or more graphs of object.
That is to say you can travel in this graph having only one entry point, example, you have the name of a customer.
You'll query to retrieve the customer Object.
Then having it you'll be able to call myCustomer.getMother().getAdress().getState().getPresident().getDog() if you want, hibernate will hit the db for you and you won't have to write a query to retrieve all this.
There is two ways to think:
- you load all the object graph when retrieving the customer.... it will retrieve all the db and won't be performant
- to avoid this, you can "lazy" load some part of the object graph which will loaded only when asked (example at calling myCustomer.getMother())
There is no GENERAL rule, your mapping files must match with your business and lazy loading is only one of powerfull things about Hibernate.
Some other improvments can be done, i think of JBossCache for example. But you must know about your technical architecture before choosing a cache.
And i really think you'll waste less time reading the doc than waiting for other people to explain you how it works.
And if you don't like reading, buy commercial introduction to Hibernate, it's really nice! ;)