Home » Example to Create Hibernate Application in MyEclipse IDE Tutorial

Example to Create Hibernate Application in MyEclipse IDE Tutorial

by Online Tutorials Library

Example to create the Hibernate Application in MyEclipse

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

  1. Create the java project
  2. Add hibernate capabilities
  3. Create the Persistent class
  4. Create the mapping file for Persistent class
  5. Add mapping of hbm file in configuration file
  6. Create the class that retrieves or stores the persistent object
  7. Add jar file for oracle
  8. 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 hibernate capabilities

To add the jar files select your project - click on MyEclipse menu - Project Capabilities - add Hibernate capabilities- next- next. Now specify the database connection details as displayed in the figure below.

example to create hibernate application in MyEclipse IDE

Here, Check the Enable dynamic table creation check box to create automatic table - next - Uncheck the create SessionFactory class because we are going to write the code to get session object self for better understanding - finish.

Now configuration file will be created automatically.


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.tutoraspire.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. Copy the dtd for this mapping file from this example after downloading it.

employee.hbm.xml


5) Add mapping of hbm file in configuration file

open the hibernate.cgf.xml file, and add an entry of mapping resource like this:

Now the configuration file will look like this:

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) Add the jar file for oracle (ojdbc14.jar)

To add the ojdbc14.jar file, right click on your project - build path - add external archives - select the ojdbc14.jar file - open.

8) Run the application

Before running the application, determine that directory structure is like this.

example to create first hibernate application in myeclipse IDE

To run the hibernate application, right click on the StoreData class - Run As - Java Application.

You may also like