Wednesday, January 16, 2013

Hibernate Tools Tutorial


You need to have the following tool installed on your machine.
  •        Eclipse
  •        JBoss Hibernate Tools plugin for eclipse
  •        Oracle jdbc driver
  •        Required Hibernate Jars


Follow the very simple steps to configure hibernate with JSF.
     

Open the Hibernate Tools perspective in Eclipse.


Go to the Hibernate Configuration Area in the left panel and upon right clicking in that panel a context menu will appear. Select the “Add Configuration” option from that menu.



Upon selecting the “Add Configuration” option a popup will appear asking for the configuration edit. Please provide the name of configuration, Configuration Type and Hibernate version.

Next Select the JSF Project with which you want to integrate hibernate configuration. and press the OK button.
 
       Now create the oracle database connection for hibernate configuration by clicking New Button.

      Upon Pressing New Button. A popup will appear with title connection profile. Select Oracle from the available list and give the connection profile a name and press Next Button.

Upon pressing Next Button the popup will ask for the Connection and Driver details. Provide the driver details as follows.




   Select the JAR List from the available tabs and provide the path of driver’s jar file.  
 Set the driver properties by clicking on Properties tab.
   Once you complete the driver’s settings. Then the driver’s information updated in the “Specify a Driver and Connection Details” popup as follows. Select the Save Password checkbox to keep remember the password and select “User” from Catalog.

Press the “Test Connection” button to test the DB connection. If the connection details are correct then you have the success message in the popup as follows.

        Press Finish button to complete the connection.


       Next Create the cfg.xml configuration file by click “Setup Button” in front of Configuration file option as follows. A new popup will appear asking for Create new configuration file or use existing configuration file. 



Upon clicking “Create New…” button, you will asked to provide the path and name of the configuration file as follows. Then press Next button.

           Upon pressing Next button. You need to provide configuration details for hibernate as follows and press Finish.


       Upon pressing “Finish”. The configuration file name loaded in the “Specify a Driver and Connection Details” popup as follows. Now press Apply and OK button to complete the configuration.

       Now you have new configuration appeared in the left panel.

      Now right click the configuration name in the left panel and select “Edit Configuration” to update the database dialect.

      Now select the database Dialect from the available list under the Options Tab. Then press Apply and OK.


       Now expand hibernate configuration node from the left panel and then expand Database node. Database schemas populated if the configuration is correct.

       From the option bar on top select the Hibernate Code Generation and select the “Hibernate Code Generation Configuration” option from the menu.


       To generate the code Select configuration that we created before, provide the path of output directory and select the check box against “Reverse engineer from JDBC Connection”, provide the optional package name then create setup button to generatge “reveng.xml” file.



        Select “Create New…”


        Select the path for reveng.xml file and provide its name and press Next button


       Press the Finish after selecting the schema to generate the code.


        Now press Apply and Run to finish the code generation.


        Now find the desired artificat at the provided location.