Thursday, December 23, 2010

Hibernate: HQL Select query on map elements

A quick tip -
How to write HQL select query on map elements:
HBM File:

<class name="TimerData" table="timer_data" lazy="false">
    <id name="userId" type="string"/>
...........
       <map name="lastExecTimeMap" lazy="false">
              <key column="userId"/>
              <map-key column="handleType" type="string"/>
              <element column="lastExecTime" type="timestamp"/>
              <!-- <element column="handle" length="1024000"/>-->
        </map>
...........
</class>


I want to get value of lastExecTime for given value of userId and handleType.
Here's the HQL query:
"select letMap
from TimerData d inner join d.lastExecTimeMap as letMap
where d.userId = ? and index(letMap)=?"

This will return lastExecTime of type Timestamp... value of 'lastExecTimeMap' for given key 'handleType'

1 comment: