For anyone who uses Eclipse and is also writing HQL, the Hibernate Tools HQL Editor is a very useful piece of software. The tool I have been using is the one provided by JBoss who also provide the popular Object Relational Mapping (ORM) tool Hibernate itself. The hibernate tools provide several utilities but Im just going to concentrate on the use of the HQL editor in this article.
The JBoss hibernate tools are a plugin for the Eclipse IDE. The first thing to do therefore is install them in Eclipse, and this is most easily done from within Eclipse using the Update Manager. At the time of writing the relevant information and update site are as follows:
information:?http://docs.jboss.org/tools/2.1.0.GA/en/hibernatetools/html/setup.html
update site:?http://download.jboss.org/jbosstools/updates/stable/
The versions of Eclipse and Hibernate Tools I am using for the purpose of this article are:
Eclipse Ganymede – 3.4.1
Hibernate Tools – 3.2.4
After you have installed Hibernate Tools in Eclipse go to Window > Open Perspective > Other > Hibernate
Now find the Hibernate Configurations view – it should open automatically with the perspective but you can also open it via Window > Show Viw> Other > Hibernate > Hibernate Configurations.
In the Hibernate Configurations view right-click (windows) on the hibernate node to open the context menu and select Add Configuration.
Main?Tab
Select your project
If you are connecting to Hibernate then leave the database connection
as [Hibernate configured connection]
Now you need to setup your configuration file – click Setup next to the Configuration file field and selct Create new.
This file configures the hibernate session basically.
You need to provide the database driver details, authentication details, mapping file locations etc
Example hibernate.cfg.xml
<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
<hibernate-configuration>
??<session-factory>
????<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
????<property name=”connection.url”>jdbc:mysql://localhost:3306/myDataBase</property>
????<property name=”connection.username”>root</property>
????<property name=”connection.password”>password</property>
????<property name=”connection.pool_size”>1</property>
????<property name=”dialect”>org.hibernate.dialect.MySQL5InnoDBDialect</property>
????<property name=”current_session_context_class”>thread</property>
????<property name=”cache.provider_class”>org.hibernate.cache.NoCacheProvider</property>
????<property name=”show_sql”>true</property>
????<mapping resource=”com/myProject/entity/Result.hbm.xml”/>
????<mapping resource=”com/myProject/entity/Tag.hbm.xml”/>
????<mapping resource=”com/myProject/entity/TaggedResult.hbm.xml”/>
??</session-factory>
</hibernate-configuration>
?
Classpath?Tab
You will need to add any dependant projects and libraries, namely the location of the database driver.
When you click OK in the Hibernate Configuration window – if all is well – the plugin should create a Session Factory and obtain a connection to the database and you should see 3 more nodes – Configuration, Session Factory and Database. If there are problems you will get some errors that you will need to resolve before you can proceed.
Once you have got the configuration right and have your Session Factory then right-click on any node and select HQL editor.
try a straightforward query to test things ie. if you have a table RESULT try typing “from Result2 in the HQL query window. Now click the green Run HQL arrow and you will be able to see the records returned in the Hibernate Query Result view (you may need to open this or it may be lurking behind other views). Youcan also see the SQL that Hibernate generates from the HQL you write in the Hibernate Dynamic SQL Preview view.
Happy HQL’ing!