Hibernate Tools is a set of tools, provided as a plugin for Eclipse, which can ease your work with Hibernate. Among other features, it includes a Object mapping generator and a HQL editor. I find the HQL editor very convenient as it can be very useful for trying and polishing queries before starting to code.
In this post, I cover how to set up Hibernate Tools, using my current working environment (NavalPlan) as example. NavalPlan is a web-based open-source planning tool. You can read more about it here.
First, install Hibernate Tools for Eclipse:
- Click on Help -> Install New Software…
- Click on ‘Add‘ to add a new repository. Location: http://download.jboss.org/jbosstools/updates/stable/
- Once all the available packages have been fetched, select ‘Hibernate Tools’ and click on ‘Next’ to proceed with the installation.
Once Hibernate Tools have been installed, click on Window -> Show View -> Others… A new window pops up, click on folder ‘Hibernate’ and select ‘Hibernate Configurations’ to setup a DB connection for Hibernate Tools.
Click on ‘+’ button at the top-left menu to create a new Configuration, and go through the following steps:
On the tab ‘Main’ fill up the following information:
- Project. Select your project (in my case NavalPlan)
- Database connection. Click on ‘New…’
- Select ‘Generic JDBC’ and click ‘Next’.
- Fill up the JDBC data for your DB. In my case, I’m using PostgreSQL:
- Database: PostgreSQL
- URL: jdbc:postgresql://localhost:5432/navaldev
- User name: naval
- Password: **
- Click on ‘Test Connection’ to ping the DB connection.
- Click on ‘Next’ and then ‘Finnish’.
Now, on the tab ‘Options’, fill up the following:
- Database dialect: PostgreSQL
Lastly, it’s necessary to feed the connection with the entity mappings. For doing that, click on the tab ‘Mappings’. Locate the main folder that contains your .hbm.xml files. Unfold any other folders within and select each .hbm.xml file of the entities you wish to import. In general, select all .hbm.xml as you may wish to query any entity of your application. This step can be a bit tedious as it’s necessary to click individually on each .hbm.xml file, however it’s possible to add new selections by pressing Ctrl + Click.
Once the .hbm.xml files have been added, click on ‘Finish’ to end the configuration. At this step, a new configuration should be created. Click on it to unfold. Hibernate Tools imports the entities selected on the previous step and you console is ready to execute HQL statements.
At the top-left menu click on the third option starting from the left, ‘Open HQL editor’. Try a simple HQL statement to check everything is working. In my case, I try a query for retrieving all Workers in NavalPlan:
SELECT worker FROM Worker worker
And that’s it!! Congratulations!! you can now try HQL queries before running any code 😉