In this tutorial you will learn that how easily you can do your first example of Hibernate 4 with Eclipse
First of all to do an example of Hibernate 4 you should have installed an appropriate JDK (JDK version 1.5 or more). About Complete tutorial of JDK download click here. Next if you want to do example in Eclipse so an Eclipse IDE (Eclipse version 3.6 or more, see the full tutorial of Eclipse from here ) must have installed in your computer properly. Next you will need jar files of Hibernate and a database connector jar file( for example if you want to use MySQL database so you will have to use mysql-connector-java-5.1.3-rc-bin.jar ) to create an example ( read how to download jar files of Hibernate from here ). And finally you should have properly installed and running MySQL database to persist the data.
Now you are able to create a Hibernate project.
Below I am going to give a very simple example which will demonstrate you how to create a hibernate project using Eclipse. Start an Eclipse select the workspace and then Select File->New -> Project -> Java Project and then click on Next button.
In the next window give the project name and click on 'Finish' button.
You will see the project is being setup with the name specified by you (in this case project is being setup with the name 'coreHibernateExample') now to create a Hibernate example add the jar files of Hibernate. To add the jar files you can follow the step given below :
Step 1 : Select your project which you want to add the Hibernate's jar files then Right click -> 'propeties'
You will be prompted to a new dialog box.
Step 2 : In this step select the Java Build Path and the Libraries tab as look in the figure given below and click on the button 'AddLibraries'.
Step 3 : When you will click on AddLibraries button an 'Add Library' dialog box will be open to you for adding library here you have to select User Library option from the drop down list as given in the figure below and then click on 'Next' button.
Step 4 : In this step you will be prompted to a new dialog box named User Libraries here click on the button New if you have not created your own libraries for Hibernate jar file. After clicking on New button, a dialog box will be appear to you for creating a new library folder give the name as you wish and click on OK button a new folder will be created with the specified name.
Step 5 : In this step you have to select this folder and click on AddJARs... button then go to your place where you have kept your Hibernate's jar files / specified path/root of Hibernate's jar files then select the jar files( to know which jar files are required to create core hibernate project click here) which are required for creating a Hibernate project as given below in the figure.
NOTE : In the above fig. you will see an extra jar file 'mysql-connector-java-5.1.3-rc-bin.jar' this file is used because we will required to connect with the database Here I am using MySQL therefore I have included this jar file you can add an another jar file of the database that you want to use.
Click on open button you will see the jar files are included in the the User Library folder that you have created as given below in the figure and then click on OK button
Step 6 : After clicking on OK button in the previous step a dialog box will be appear to you here you have to click on FINISH button and then OK. You will see the jar file folder will be added in your project with name specified by you as given below in the figure.
Now after completed the preliminary work you are able to do an example of Hibernate 4 with Eclipse.
To see the complete example of Hibernate 4 click here
Posted on: February 16, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles