Javatpoint Logo
Javatpoint Logo

Hibernate Example using XML in Eclipse

Here, we are going to create a simple example of hibernate application using eclipse IDE. For creating the first hibernate application in Eclipse IDE, we need to follow the following steps:

  1. Create the java project
  2. Add jar files for hibernate
  3. Create the Persistent class
  4. Create the mapping file for Persistent class
  5. Create the Configuration file
  6. Create the class that retrieves or stores the persistent object
  7. Run the application

1) Create the java project

Create the java project by File Menu - New - project - java project . Now specify the project name e.g. firsthb then next - finish .


2) Add jar files for hibernate

To add the jar files Right click on your project - Build path - Add external archives. Now select all the jar files as shown in the image given below then click open.

Download the required jar file

example to create hibernate application in Eclipse IDE

In this example, we are connecting the application with oracle database. So you must add the ojdbc14.jar file.


download the ojdbc14.jar file

3) Create the Persistent class

Here, we are creating the same persistent class which we have created in the previous topic. To create the persistent class, Right click on src - New - Class - specify the class with package name (e.g. com.javatpoint.mypackage) - finish .

Employee.java


4) Create the mapping file for Persistent class

Here, we are creating the same mapping file as created in the previous topic. To create the mapping file, Right click on src - new - file - specify the file name (e.g. employee.hbm.xml) - ok. It must be outside the package.

employee.hbm.xml


5) Create the Configuration file

The configuration file contains all the informations for the database such as connection_url, driver_class, username, password etc. The hbm2ddl.auto property is used to create the table in the database automatically. We will have in-depth learning about Dialect class in next topics. To create the configuration file, right click on src - new - file. Now specify the configuration file name e.g. hibernate.cfg.xml.

hibernate.cfg.xml


6) Create the class that retrieves or stores the persistent object

In this class, we are simply storing the employee object to the database.


7) Run the application

Before running the application, determine that directory structure is like this.
example to create first hibernate application in eclipse IDE
To run the hibernate application, right click on the StoreData class - Run As - Java Application.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA