Sunday, April 27, 2008

Hibernate Tools - 101 (Eclipse IDE)

First Draft:

My first take on Hibernate Tools, also this being my first post, comments and suggestions are very well appreciated:

Installing and setting up Hibernate Tools was not easy. Most of it, I blame it on my inexperience with installing Eclipse plugins and problem finding a way to setup the tools from the original website: http://www.hibernate.org/255.html.

If you start browsing the above website now, you should probably end up at this link after spending a decent time searching for the right way to install the tools (assuming you too do not know how to install plugins Eclipse). Anyways, let me guide you with the installation and a step by step procedure to get started to get the most out of Hibernate Tools.

I am assuming you have downloaded the latest free Eclipse IDE, the one for the Java EE developers. This one is really neat with cool features such its new JavaEE perspective that allows actually preview how the JSPs appear in the web page without requiring to recompile and then opening the JSP in the browser.

Well, here's the step 1:
Download the Latest release as of 27 April, 2008
Latest release: 3.2.2.Beta1
I am not sure where stable versions are available, but you should note that the Hibernate Tools is coming from the sourceforge.nets JBoss project file downloads. I initially searched sourceforge.net for Hibernate Tools only to find nothing. So essentially the downloads are coming from the project http://sourceforge.net/projects/jboss/

Unzip the downloaded .rar file using any application which can uncompress rar files, google it if you don't have one :-? Once unzipped, you will have two folders features/ and plugins/ in the root folder. Copy these two folders as is into the folder where you have installed Eclipse. This will be the /eclipse/ folder itself where you will also find /features and /plugins folders. Thats it! you are good to go. While writing this post, I am quite amazed at the significant improvement in the quality and content in the Hibernate Tools Reference that you will probably stop reading further. With so many changes taking place every month his post will rather become outdated and useless as I am writing it. Going forward, if you still cannot detect any new tools, hibernate perspective or other things, you should try the following Hibernate reference suggests:

Sometimes Eclipse does not automatically detect new plugins and thus the tools will not be activated. To ensure eclipse sees these changes just clean up the cached plugin information by running eclipse with the -clean option, e.g. eclipse -clean. Using the update site does not require any additional steps.

Check out the Running Eclipse section for some information on how to start Eclipse with various options.

Now coming to the best part and rather the actual reason for my post, setup your Eclipse's very own Hibernate HQL query console, use reverse engineering tools to get the mapping files (.hbm.xml), object file (.java file) in a single click from complete database schema and much more.

Step 2:

You now have a Hibernate perspective setup in your Eclipse. Open the hibernate perspective by clicking the Window/Open perspective/Other and then choose Hibernate. The hibernate perspective creates an icon with a build/start button with the hibernate symbol on the top right just where your ant build, and debug icons can be found. Clicking this symbol provides you with a 'Open Hibernate Code Generation Dialog' option which can be used to automatically export the object files, hibernate XML mapping files, DAO, and much more. Assuming that you have a hibernate project setup in your Eclipse. Before we proceed further enable the views by clicking Window/show view and then choose the following views which will be used later: Hibernate configurations, Hibernate query result, Hibernate Dynamic SQL preview, all of these are available within the Hibernate option.

Now the first goal is to add a new hibernate configuration. In the Hibernate configuration, click the plus symbol to open up a new window that creates a new hibernate configuration. This configuration is usually per application or project although there is no limitation on the number of configurations that you can create. Give a proper name, I usually prefer giving the same name as that of the project. In the Project, choose Browse and assuming that your hibernate project is already available in the list of available projects in your eclipse workspace, select the appropriate project (make sure that the project is open and not closed). Leave the property file as is in case if you too have put all of the configuration information in the hibernate.cfg.xml rather than in a property file. In the configuration file, browse the project that you chose in the Project window to select the hibernate.cfg.xml or any other file that you have chosen as the parent configuration file ie, the one which has all of your database connnection details and is used to create your session factory. These are the minimum steps to create a new hibernate configuration.

Tip: Please also open the Properties and Error log views open. The properties view displays complete details on an entity that is selected. The error log provides complete details on any error that occurs including a possible solution to the problem.

By clicking the (+) symbol on the hibernate configuration opens the
1. Configuration: All the database object files in the order of their names. Right click and choose the cool features to be able to access the mapping diagram (database - mapping file relationship), actual source file (.java object file), and the mapping file (hibernate .xml file). The more recent version of hibernate tools allows you to save or export this database-mapping relationship as an image. This is pretty useful view if you have defined the many-to-one, one-to-many relationships for this entity as the view shows all the tables and the relationships.
2. Session Factory: Double clicking any of the object files in the session factory actually runs a select query displaying all the records from the corresponding database table. The results as a list of object files can be seen in the Hibernate Query Result view that you have already opened.
Selecting any of the objects allows you to view the values of all the fields/properties available in the object in the Properties view.
3. Database: Fetches the database table details. While fetching the table details, doing any thing in Eclipse else actually shuts down Eclipse. This view allows you to directly access the table details whose mapping files have been added to your parent configuration file.

In case you make changes to any of the mapping files or to the configuration file, you can always refresh and rebuild the configuration. Another useful thing is the ability to run a schemaExport ;) i.e., you can just change the database dialect and get the schema out, in case you donot have the schema when your starting point is the mapping file. The schema export allows you to create your database tables in any dialect.

Step 3:

Let's get your liking for hibernate tools increase further. Open the HQL editor by clicking the HQL symbol in the same hibernate configurations window. Select your hibernate configuration and click the start symbol in green to create the session factory in order to be able to run any HQL query. Any errors while creating session factory can be understood from Error Log view. If you do not do this you probably might not be able to use the context help, i.e., let's say you type ctrl + space and you get to choose from a list of possible commands or functions (all supported HQL functions including others such as from, select,... to start your query) and you choose from to write the from someObj as the HQL query. You will not find the available objects by pressing ctrl + space unless you have the configuration loaded by creating the session factory. Anyways, let us go with the basic HQL query first. Try typing from and press ctrl+space to get all available objects to choose from, provide an alias name from someObj someObjAlias then start adding the select statement as in select someObjAlias. with a '.' and a ctrl+space to get all available fields for the selected object. Continue to write your query and while you do that use the dynamic SQL query and Error Log views to verify and debug your HQL queries.

The more recent version of Hibernate tools allows you to right click any property or object and start writing the HQL query on this property or object. Some of the tips here include, formatting your HQL by right clicking in the HQL editor and selecting format HQL or using the short cut ctrl+shift+F. Another tip includes saving the HQL editor as a file that can be opened later. You can also mention the number of results that you want to see in order to avoid the problem of burdening the database by fetching way too many records. In order to run a dynamic HQL by mentioning the parameters required for the HQL you use the

from someObj someObjALias where someObjAlias.someDate =:someDate

In order to provide these parameters you have to open the Query Parameters view and enable the paramters by clicking the symbol :P+ and the editor is smart enough to add all the properties that are being used in the HQL query. You now have to mention the editor what kind of data type the query parameter expects; they could have provided the default data type using reflection from the actual property on which the query parameter is passed. And then mention the values, the date has to be mentioned as 01 jan 2005 unlike 01-JAN-2005. Do not forget to disable the query parameters by clicking the right most icon in its view when you are not using the query paramters.

As discussed earlier, the results in the Hibernate Query Results View can be easily previewed in the Properties view. Even the parent objects and their property values can be viewed. Interestingly, they did not provide a way to save this result, not even a copy/paste is available!







Never forget the references and rather better places to learn more:


The coming weeks, I will be posting more step by step procedures:
1. Using hibernate for Jasper Reports
2. Debugging with Tomcat + Eclipse
3. Stop using alerts, use better Java Script debugging tools

No comments: