Hibernate Queries - I

2 May 2008

Hibernate is a popular OR (Object Relational) mapping framework. To access database, we have to use Hibernate Query language. I will present the basics in this post that will help you starting quering database tables using HQL.

Hibernate queries can be roughly divided into 3 categories:

The queries we general use can be divided up into three main categories :

- Queries using one class
- Queries which several classes so join operation is required
- Queries which cannot be efficiently done using joins

Lets take an example of simplest HQL query:

from Sale sale where sale.date > :startDate

Let me present a query with join operation:

from Cat as cat 
 inner join cat.mate as mate
 left outer join cat.kittens as kitten

A little tricky example follows:

from Cat as cat 
    inner join fetch cat.mate
    left join fetch cat.kittens child
    left join fetch child.kittens

Consider the following (simplified) Hibernate mappings:

 
<class name="Sale" dynamic-update="true" table="t_sale" >
 
	<id column="sale_ch_id" name="id" unsaved-value="null"
		 type="string">
 
		<generator class="uuid.hex"/>
	</id>
 
	<property column="sale_d_date" name="date" type="date"
		not-null="true"/>
	<property column="sale_n_price" name="price"
		type="big_decimal" not-null="true"/>
	<many-to-one name="product" class="Product"
		column="prod_ch_id".../>
	...
</class>

continued …

del.icio.us:Hibernate Queries - I  digg:Hibernate Queries - I  spurl:Hibernate Queries - I  wists:Hibernate Queries - I  simpy:Hibernate Queries - I  newsvine:Hibernate Queries - I  blinklist:Hibernate Queries - I  furl:Hibernate Queries - I  reddit:Hibernate Queries - I  fark:Hibernate Queries - I  blogmarks:Hibernate Queries - I  Y!:Hibernate Queries - I  smarking:Hibernate Queries - I  magnolia:Hibernate Queries - I  segnalo:Hibernate Queries - I  gifttagging:Hibernate Queries - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Hibernate Queries - I

Leave a Reply